【发布时间】:2019-04-30 11:31:31
【问题描述】:
我有 2 个对象数组,我想对文章作者进行匹配并推送到一个新数组
var result4 = [{
path: "don-t-call-me",
details: {
articleTitle: "Don’t call me",
articleAuthor: "Dave Wood",
}
},
{
path: "diary",
details: {
articleTitle: "Diary",
articleAuthor: "Alan Johnson",
}
}
}]
var result3 = [{
path: "letters",
letters: 7
},
{
path: "dont-call-me",
details: {
articleTitle: "Don’t call me"
articleAuthor: "Dave Wood",
}
}, {
path: "reduced-to-ashes-and-rubbage",
details: {
articleTitle: "Reduced to rubble",
articleAuthor: "Jack Jones",
}
}, {
path: "diary-for-2018",
details: {
articleTitle: "Diary for 1998",
articleAuthor: "Alan Johnson",
}
}
]
因此我希望输出为
var newArr1 = [{
path: "don-t-call-me",
details: {
articleTitle: "Don’t call me",
articleAuthor: "Dave Wood",
}
},
{
path: "diary",
details: {
articleTitle: "Diary",
articleAuthor: "Alan Johnson",
}
}
}]
var newArr2 = [
{
path: "dont-call-me",
details: {
articleTitle: "Don’t call me"
articleAuthor: "Dave Wood",
}
}, {
path: "diary-for-2018",
details: {
articleTitle: "Diary for 1998",
articleAuthor: "Alan Johnson",
}
}
]
目前我从每个数组中的每个元素的每个元素的详细信息对象中创建一个集合
var set3 = new Set(result3.map(({ details }) => details));
var set4 = new Set(result4.map(({ details }) => details));
然后使用 set.has() 创建一个新数组
var newArr1 = result3.filter(({ articleAuthor }) => set4.has(articleAuthor));
var newArr2 = result4.filter(({ articleAuthor }) => set3.has(articleAuthor));
newArr2 的输出正确,但 newArr1 为空
【问题讨论】:
标签: javascript arrays nested javascript-objects