Javascript:无法基于2个数组中匹配的嵌套值创建数组

时间:2019-04-30 11:31:32

标签: javascript arrays nested javascript-objects

我有2个对象数组,我想对文章Au​​thor进行匹配并推送到新数组

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",
        }
    }
]

当前,我从每个数组创建一个集合,每个数组仅包含来自每个数组中每个元素的details对象。

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为空

1 个答案:

答案 0 :(得分:4)

您需要对作者进行更深层次的破坏,

{ details: { articleAuthor } = {} }

因为否则,您将对象detail用作Set,并且该值在每个对象文字中都是唯一的。而且您不会得到articleAuthor

var result4 = [{ path: "don-t-call-me", details: { articleTitle: "Don’t call me", articleAuthor: "Dave Wood" } }, { path: "diary", details: { articleTitle: "Diary", articleAuthor: "Alan Johnson" } }],
    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" } }],
    set3 = new Set(result3.map(({ details: { articleAuthor } = {} }) => articleAuthor)),
    set4 = new Set(result4.map(({ details: { articleAuthor } = {} }) => articleAuthor)),
    newArr1 = result3.filter(({ details: { articleAuthor } = {} }) => set4.has(articleAuthor)),
    newArr2 = result4.filter(({ details: { articleAuthor } = {} }) => set3.has(articleAuthor));

console.log(newArr1);
console.log(newArr2);
console.log([...set3]);
console.log([...set4]);
.as-console-wrapper { max-height: 100% !important; top: 0; }