[英]lodash unable to flatten array
I am trying to unnest an array currently using this code:我正在尝试取消嵌套当前使用此代码的数组:
var mapped = _.flatMap(payload, ({GroupId,Learners}) =>
_.map(Learners, LearnerId => ({GroupId:GroupId,Learner:LearnerId}))
);
however the value return still produces and array: eg然而值返回仍然产生和数组:例如
{"GroupId":1246574,"Learner":{"Forename":"George","Surname":Bob",,"StartDate":"2020-09-01","EndDate":"2021-08-27"}}
How can I adapt the script above to only return the GroupId and the Surname?如何修改上面的脚本以仅返回 GroupId 和 Surname?
Example of the payload is here:有效载荷的示例在这里:
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}))
})
You can access access the elements as following:您可以按以下方式访问访问元素:
console.log(payload.GroupId);
var learners = payload.Learners;
learners.forEach(function(learner){
console.log(learner.Surname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.