从javascript中的嵌套数组过滤数组

时间:2019-06-24 12:48:39

标签: javascript multidimensional-array

我有两个数组,我需要根据名称对其进行过滤。

const arr1 = ["Saab", "Volvo", "BMW", "Car3", "Car2", "Van", "Bus"];
const arr2 = [
    {
        name: "Saab",
        url: "saab/"
    },
    {
        name: "Volvo",
        url: "volvo/",
        children:[
            {
                name: "Van",
                url: "van/"
            },
            {
                name: "Bus",
                url: "bus/"
            },
        ]
    },
    {
        name: "BMW",
        url: "bmw/"
    },
    {
        name: "SUV",
        url: "suv/",
        children:[
            {
                name: "Car1",
                url: "car1/"
            },
            {
                name: "Car2",
                url: "car2/"
            },
            {
                name: "Car3",
                url: "car3/"
            },
        ]
    }
]

我尝试使用此代码。 const result = arr2.filter((item) => arr1.includes(item.name);

此代码可以正常工作,但它不会检查arr2中的子对象,我需要根据arr1值进行检查,如果我有子对象,则还需要检查该名称。例如:名称SUVarr1中,但是car1car2的值不在arr1中,那时候我需要删除该SUV对象。

我的输出应该是:

[
    {
        name: "Saab",
        url: "saab/"
    },
    {
        name: "Volvo",
        url: "volvo/",
        children:[
            {
                name: "Van",
                url: "van/"
            },
            {
                name: "Bus",
                url: "bus/"
            },
        ]
    },
    {
        name: "BMW",
        url: "bmw/"
    },
]

如何使用javascript进行操作,是否可以在es6中进行操作?

3 个答案:

答案 0 :(得分:2)

您可以使用另一个过滤器功能来检查过滤器功能内的children

说明:

  • 检查itemitem.length以确保子代存在并且不为空(如果不需要,您可以删除第二个子代,但是这样做比较安全,因为在大多数情况下,它可以确保子代是数组,并且.every不会引发错误)
  • .every.includes测试是否所有孩子都在arr1
  • 对于带有||的替代项的第二部分,如果第一部分为true,则不会执行
  • 在第二部分中,您必须测试!item.children,因为如果第一个选择对孩子失败,它将退回到arr1.includes(item.name)的结果上,而“ SUV”将返回结果

const arr1 = ["Saab", "Volvo", "BMW", "SUV", "Car3", "Van", "Bus"];
const arr2 = [
    {
        name: "Saab",
        url: "saab/"
    },
    {
        name: "Volvo",
        url: "volvo/",
        children:[
            {
                name: "Van",
                url: "van/"
            },
            {
                name: "Bus",
                url: "bus/"
            },
        ]
    },
    {
        name: "BMW",
        url: "bmw/"
    },
    {
        name: "SUV",
        url: "suv/",
        children:[
            {
                name: "Car1",
                url: "car1/"
            },
            {
                name: "Car2",
                url: "car2/"
            },
            {
                name: "Car3",
                url: "car3/"
            },
        ]
    }
];

const result = arr2.filter( (item) => ( (item.children && item.children.length && item.children.every(v => arr1.includes(v.name))) || (!item.children && arr1.includes(item.name)) ) );

console.log(result);

编辑:满足要求的固定代码

EDIT2 :变成了纯ES6答案,现在不再需要老式的

答案 1 :(得分:0)

您可以对结果使用foreach并通过最后一种方法检查项目值(过滤嵌套数组)。

答案 2 :(得分:0)

请尝试这个。

res = arr2.filter(item2 => (arr1.findIndex(item1 => item2['name'] == item1) > -1))