我有像这样的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'
}
]
}
答案 0 :(得分: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;
});
-->
&#13;
答案 1 :(得分:0)
项有两个属性,国家和短。您正尝试在项目上运行.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);
答案 2 :(得分:0)
您可以使用filter
功能仅返回符合条件的项目。像这样:
const matches = [];
continents.forEach((continent) => {
const countries = continent.countries.filter((country) => {
return country.name.includes('col');
});
matches.push(...countries);
});
注意:我使用spread
运算符...
来平坦化数组(避免使用数组数组)。
答案 3 :(得分: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);
答案 4 :(得分: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'));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;