JavaScript:在对象数组中过滤包含数组属性的对象

3
我有一个像这样的JS对象:
var continents = [
0: {
       short: 'na',
       countries: [
           {
               name: 'canada'
           },
           {
               name: 'usa'
           },
           //...
       ]

   },
   1: {
       short: 'sa',
       countries: [
           {
               name: 'chile'
           },
           {
               name: 'colombia'
           }
       ]

     },
//...
]

我希望筛选出该对象中与国家名称(contents.countries.name)匹配的某些字符串(例如“col”) 示例筛选函数

filter(item => {
    return item.name.toLowerCase().indexOf('col'.toLowerCase()) >= 0;
});

期望的结果:

  1: {
       short: 'sa',
       countries: [
           {
               name: 'colombia'
           }
       ]
     }
5个回答

1
你需要对大洲和其中的国家进行筛选。
这是一个两部分的筛选,如下所示。

var continents = [{
  short: 'na',
  countries: [
    { name: 'canada' },
    { name: 'usa' }
  ]
}, {
  short: 'sa',
  countries: [
    { name: 'chile' },
    { name: 'colombia' }
  ]
}];

function filterByKeyValue(arr, keys, val) {
  return arr.filter(item => {
    return item[keys[0]].some(subitem => subitem[keys[1]].indexOf(val) > -1);
  }).map(item => {
    item[keys[0]] = item[keys[0]].filter(subitem => subitem[keys[1]].indexOf(val) > -1);
    return item;
  });
}

var filtered = filterByKeyValue(continents, [ 'countries', 'name' ], 'col');

console.log(filtered);
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!--

Original filter that method is based on.

var filtered = continents.filter(continent => {
  return continent.countries.some(country => country.name.indexOf('col') > -1);
}).map(continent => {
  continent.countries = continent.countries.filter(country => country.name.indexOf('col') > -1);
  return continent;
});

-->


0

你可以尝试对国家进行筛选,如果找到一个国家,也将外部大陆推送到结果集中。

function getItems(name) {
    var result = [];

    continents.forEach(function (continent) {
        var countries = continent.countries.filter(function (country) {
            return country.name.startsWith(name);
        });
        if (countries.length) {
            result.push({ short: continent.short, countries: countries });
        }
    });
    return result;
}

var continents = [{ short: 'na', countries: [{ name: 'canada' }, { name: 'usa' }] }, { short: 'sa', countries: [{ name: 'chile' }, { name: 'colombia' }] }];

console.log(getItems('col'));
.as-console-wrapper { max-height: 100% !important; top: 0; }


0

在这种情况下,item有两个属性,countries和short。您正在尝试在item上运行.name,但该属性不存在。您需要在item.countries [0] .name或item.countries [1] .name上运行.name。

    var continents = [
        {
            short: 'na',
            countries: [
                {
                    name: 'canada'
                },
                {
                    name: 'usa'
                }
            ]

        },
        {
            short: 'sa',
            countries: [
                {
                    name: 'chile'
                },
                {
                    name: 'colombia'
                }
            ]

        }
    ];

    var results = continents.filter(item => {
        return item.countries[0].name.toLowerCase().indexOf('col'.toLowerCase()) >= 0 || 
item.countries[1].name.toLowerCase().indexOf('col'.toLowerCase()) >= 0
    });

    console.log(results);

工作示例:https://jsfiddle.net/gugui3z24/fnw5kf0x/

是的。我有6个大陆和超过250个国家。我正在寻找如何循环遍历它们并进行筛选的解决方案。 - omnomah

0
你可以使用filter函数来返回符合条件的项目。就像这样:
const matches = [];
continents.forEach((continent) => {

  const countries = continent.countries.filter((country) => {
    return country.name.includes('col');
  });

  matches.push(...countries);
});

注意:我已经使用 spread运算符...来展开数组(避免了数组中包含其他数组)。

0
您可以使用以下代码来进行循环:

var continents = [
    {
        short: 'na',
        countries: [
            {
                name: 'canada'
            },
            {
                name: 'usa'
            }
        ]
    },
    {
        short: 'sa',
        countries: [
            {
                name: 'chile'
            },
            {
                name: 'colombia'
            }
        ]
    }
];

var results = continents.filter(item => {
    for (let x = 0; x < item.countries.length; x++) {
        if (item.countries[x].name.toLowerCase().indexOf('usa') > -1) {
            return item;
            break;
        }
    }
});

console.log(results);

工作示例:https://jsfiddle.net/gugui3z24/fnw5kf0x/2/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接