【问题标题】:Javascript: Unable to create array based on matching nested values in 2 arraysJavascript:无法根据 2 个数组中的匹配嵌套值创建数组
【发布时间】: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


    【解决方案1】:

    你需要对作者进行更深层次的解构,

    { 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; }

    【讨论】:

    • [...set3] 中的undefined 是故意的吗?
    • 其实是的,因为result3 中的第一个对象没有详细对象,并且只要没有articleAuthor 的值undefined(没有字符串),那么它应该可以正常工作。
    • @briosheje 导致 undefined 的对象与数组中的其余对象具有不同的结构,因此设置 3 是正确的
    • @DrumBongo 是的,只是想知道它是否有意;)
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 2013-12-31
    • 2021-12-29
    • 2017-03-18
    • 2021-05-23
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多