[英]lodash unable to flatten array
我正在嘗試取消嵌套當前使用此代碼的數組:
var mapped = _.flatMap(payload, ({GroupId,Learners}) =>
_.map(Learners, LearnerId => ({GroupId:GroupId,Learner:LearnerId}))
);
然而值返回仍然產生和數組:例如
{"GroupId":1246574,"Learner":{"Forename":"George","Surname":Bob",,"StartDate":"2020-09-01","EndDate":"2021-08-27"}}
如何修改上面的腳本以僅返回 GroupId 和 Surname?
有效載荷的示例在這里:
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}))
})
您可以按以下方式訪問訪問元素:
console.log(payload.GroupId);
var learners = payload.Learners;
learners.forEach(function(learner){
console.log(learner.Surname);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.