Javascript lodash无法展平阵列
我正在尝试使用以下代码卸载当前使用的数组:Javascript lodash无法展平阵列,javascript,lodash,Javascript,Lodash,我正在尝试使用以下代码卸载当前使用的数组: var mapped = _.flatMap(payload, ({GroupId,Learners}) => _.map(Learners, LearnerId => ({GroupId:GroupId,Learner:LearnerId})) ); 但是,返回的值仍然生成和数组: e、 g 我如何调整上面的脚本以只返回GroupId和姓氏 有效载荷的示例如下所示: var payload = { "Grou
var mapped = _.flatMap(payload, ({GroupId,Learners}) =>
_.map(Learners, LearnerId => ({GroupId:GroupId,Learner:LearnerId}))
);
但是,返回的值仍然生成和数组:
e、 g
我如何调整上面的脚本以只返回GroupId和姓氏
有效载荷的示例如下所示:
var payload =
{
"GroupId": 1208864,
"Code": "Ext-StuOth",
"GroupType": "AcademicHouseGroup",
"Learners": [
{
"LearnerId": 245218,
"LearnerCode": "2009-0017",
"Forename": "Sanelisiwe",
"Surname": "Nazi-Angileh",
"LearnerGroupLearnerId": 14719301,
"StartDate": "2017-03-09",
"EndDate": null
},
{
"LearnerId": 246638,
"LearnerCode": "2013-0076",
"Forename": "Randal",
"Surname": "Bodziarczyk",
"LearnerGroupLearnerId": 14650361,
"StartDate": "2017-02-20",
"EndDate": null
},
{
"LearnerId": 245253,
"LearnerCode": "109056",
"Forename": "Wafah",
"Surname": "Dadisman",
"LearnerGroupLearnerId": 14650360,
"StartDate": "2017-02-20",
"EndDate": null
}
},{
"GroupId": 1226419,
"Code": "U6-JD",
"GroupType": "RegistrationGroup",
"Learners": [
{
"LearnerId": 245507,
"LearnerCode": "110015",
"Forename": "Nahomy",
"Surname": "Osburn",
"LearnerGroupLearnerId": 14918706,
"StartDate": "2017-09-07",
"EndDate": "2018-07-31"
},
{
"LearnerId": 245994,
"LearnerCode": "111624",
"Forename": "Sherhona",
"Surname": "Dobritoiu",
"LearnerGroupLearnerId": 14707593,
"StartDate": "2017-08-01",
"EndDate": "2018-07-31"
}
}
它不是数组,而是(包含数组的)
您可以按以下方式访问元素:
console.log(payload.GroupId);
var学习者=有效载荷。学习者;
学习者。forEach(函数(学习者){
console.log(学习者姓名);
请修复问题的格式请添加一个(包括输入和预期输出)来显示实际问题。您的有效负载是什么?我添加了有效负载示例Hopping playload=[…yourgiven json]这会产生一个缺失值。缺失了什么
var payload =
{
"GroupId": 1208864,
"Code": "Ext-StuOth",
"GroupType": "AcademicHouseGroup",
"Learners": [
{
"LearnerId": 245218,
"LearnerCode": "2009-0017",
"Forename": "Sanelisiwe",
"Surname": "Nazi-Angileh",
"LearnerGroupLearnerId": 14719301,
"StartDate": "2017-03-09",
"EndDate": null
},
{
"LearnerId": 246638,
"LearnerCode": "2013-0076",
"Forename": "Randal",
"Surname": "Bodziarczyk",
"LearnerGroupLearnerId": 14650361,
"StartDate": "2017-02-20",
"EndDate": null
},
{
"LearnerId": 245253,
"LearnerCode": "109056",
"Forename": "Wafah",
"Surname": "Dadisman",
"LearnerGroupLearnerId": 14650360,
"StartDate": "2017-02-20",
"EndDate": null
}
},{
"GroupId": 1226419,
"Code": "U6-JD",
"GroupType": "RegistrationGroup",
"Learners": [
{
"LearnerId": 245507,
"LearnerCode": "110015",
"Forename": "Nahomy",
"Surname": "Osburn",
"LearnerGroupLearnerId": 14918706,
"StartDate": "2017-09-07",
"EndDate": "2018-07-31"
},
{
"LearnerId": 245994,
"LearnerCode": "111624",
"Forename": "Sherhona",
"Surname": "Dobritoiu",
"LearnerGroupLearnerId": 14707593,
"StartDate": "2017-08-01",
"EndDate": "2018-07-31"
}
}
var result = _.flatMap(payload, ({GroupId, Learners}) => {
return _.map(Learners, learner => ({GroupId, LearnerId: learner.LearnerId}))
})