gpt4 book ai didi

javascript - 如何合并多维数组?

转载 作者:行者123 更新时间:2023-12-03 00:31:53 27 4
gpt4 key购买 nike

这里我有 data.payloadfolderObjs 我想将 data.payload 合并到 folderObjsfolderObjsfolderid == 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);

关于javascript - 如何合并多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com