在这里,我有data.payload
和folderObjs
我想在data.payload
中合并folderObjs
,其中folderObjs
的folderid == data.payload[i].upperfolderid
参见patternObj
控制台,键入我想要的结果
我尝试使用concat,但它在folderObjs
数组中合并,而不在特定的folderObjs
folderid
中合并
data.payload
(2) [{…}, {…}]
0: { folderid: 903, folderName: "f11", upperfolderid: 899}
1: { folderid: 904, folderName: "f22", upperfolderid: 899}
folderObjs
(4) [{…}, {…}, {…}, {…}]
0: { folderid: 899, folderName: "f1", upperfolderid: 122}
1: { folderid: 900, folderName: "f2", upperfolderid: 122}
2: { folderid: 901, folderName: "f3", upperfolderid: 122}
3: { folderid: 902, folderName: "f4", upperfolderid: 122}
// TS
folderObjs : Folder[] = [];
patternObj : Pattern[] = [];
this.patterObj = this.folderObjs.concat(data.payload);
console.log(this.patternObj);
// This type result I want
0: {folderid: 899, folderName: "f1,upperfolderid: 122,
{0: {folderid: 903, folderName: "f11", upperfolderid: 899}
1: {folderid: 904, folderName: "f22", upperfolderid: 899}
}
}
1: {folderid: 900, folderName: "f2",upperfolderid: 122}
2: {folderid: 901, folderName: "f3",upperfolderid: 122}
3: {folderid: 902, folderName: "f4",upperfolderid: 122}
最佳答案
我希望下面的例子会有所帮助。
const result = this.folderObjs.map(val => {
return Object.assign({}, val, this.payload.filter(v => v.upperfolderid === val.folderid));
});
console.log(result);