繁体   English   中英

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

[英]Javascript: Unable to create array based on matching nested values in 2 arrays

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

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

{ details: { articleAuthor } = {} }

因为如果没有,您将获取Set的对象detail ,并且该值在每个对象文字中都是唯一的。 而且您没有获得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; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM