Javascript检查数组中是否存在JSON数组中的所有项



将所有数组项比较为JSON数组对象

const filterArray = ['Ford', 'Fiat'];
const showrooms = [{
"Name": "Mark Auto",
"Location": "Delhi",
"Cars": ['Ford', 'BMW', 'Fiat']
},
{
"Name": "Cardekho",
"Location": "Mumbai",
"Cars": ['Ford', 'Fiat']
},
{
"Name": "Tata Arena",
"Location": "Pune",
"Cars": ['Ford', 'BMW']
},
{
"Name": "Nexa Showroom",
"Location": "Noida",
"Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
}
]

我写代码来过滤数据,但不起作用。

const result = this.showrooms.filter(c=> this.filterArray.includes(c.Cars)).map(a=>a.Name);

所需输出

['Mark Auto', 'Cardekho', 'Nexa Showroom']

您可以使用every检查每个展厅的汽车是否包括filterArray中的所有汽车

const filterArray = ['Ford', 'Fiat'];
const showrooms = [
{
Name: "Mark Auto",
Location: "Delhi",
Cars: ["Ford", "BMW", "Fiat"],
},
{
Name: "Cardekho",
Location: "Mumbai",
Cars: ["Ford", "Fiat"],
},
{
Name: "Tata Arena",
Location: "Pune",
Cars: ["Ford", "BMW"],
},
{
Name: "Nexa Showroom",
Location: "Noida",
Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
},
];
const res = showrooms
.filter((s) => filterArray.every((c) => s.Cars.includes(c)))
.map((s) => s.Name);
console.log(res);

使用every-

const filterArray = ['Ford', 'Fiat'];
const showrooms = [{
"Name": "Mark Auto",
"Location": "Delhi",
"Cars": ['Ford', 'BMW', 'Fiat']
},
{
"Name": "Cardekho",
"Location": "Mumbai",
"Cars": ['Ford', 'Fiat']
},
{
"Name": "Tata Arena",
"Location": "Pune",
"Cars": ['Ford', 'BMW']
},
{
"Name": "Nexa Showroom",
"Location": "Noida",
"Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
}
];
const result = showrooms.filter(c => filterArray.every(e => c.Cars.includes(e))).map(a => a.Name);
console.log(result);

只需要更新过滤器内部的条件。电流未按预期结果返回。

const filterArray = ["Ford", "Fiat"];
const showrooms = [
{
Name: "Mark Auto",
Location: "Delhi",
Cars: ["Ford", "BMW", "Fiat"],
},
{
Name: "Cardekho",
Location: "Mumbai",
Cars: ["Ford", "Fiat"],
},
{
Name: "Tata Arena",
Location: "Pune",
Cars: ["Ford", "BMW"],
},
{
Name: "Nexa Showroom",
Location: "Noida",
Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
},
];
const result = showrooms
.filter((c) => {
let existedInArray = true

for(let i=0; i < filterArray.length; i++){
if(!c.Cars.includes(filterArray[i])) existedInArray = false
}
return existedInArray
})
.map((a) => a.Name);

console.log(result)

const filterArray = ["Ford", "Fiat"];
const showrooms = [
{
Name: "Mark Auto",
Location: "Delhi",
Cars: ["Ford", "BMW", "Fiat"],
},
{
Name: "Cardekho",
Location: "Mumbai",
Cars: ["Ford", "Fiat"],
},
{
Name: "Tata Arena",
Location: "Pune",
Cars: ["Ford", "BMW"],
},
{
Name: "Nexa Showroom",
Location: "Noida",
Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
},
];
const result = showrooms
.filter((c) => {
let existedInArray = true

for(let i=0; i < filterArray.length; i++){
if(!c.Cars.includes(filterArray[i])) existedInArray = false
}
return existedInArray
})
.map((a) => a.Name);

console.log(result)

我认为这将工作

您可以使用reduce+every在一个循环中解决此问题。当你把过滤器和地图结合起来做一些事情时,减少真的是一个好方法。

const filterArray = ['Ford', 'Fiat'];
const showrooms = [{
"Name": "Mark Auto",
"Location": "Delhi",
"Cars": ['Ford', 'BMW', 'Fiat']
},
{
"Name": "Cardekho",
"Location": "Mumbai",
"Cars": ['Ford', 'Fiat']
},
{
"Name": "Tata Arena",
"Location": "Pune",
"Cars": ['Ford', 'BMW']
},
{
"Name": "Nexa Showroom",
"Location": "Noida",
"Cars": ['Suzuki', 'Ford', 'Tata', 'Fiat']
}
]

const result = showrooms.reduce((prev, curr) => {
const isFilterCarsIncluded = filterArray.every(car => curr.Cars.includes(car));
if (isFilterCarsIncluded) {
return [...prev, curr.Name];
}
return prev;
}, []);
console.info(result);

演示:

showrooms
.filter(showroom => filterArray.every(item => showroom.Cars.includes(item)))
.map(showroom => showroom.Name);

完成。

使用reduce在一次迭代中,我们可以累积所需的输出

const combine = (arr, filters) =>
arr.reduce((acc, { Name, Cars }) => {
filters.every((car) => Cars.includes(car)) && acc.push(Name);
return acc;
}, []);
const filterArray = ["Ford", "Fiat"];
const showrooms = [
{
Name: "Mark Auto",
Location: "Delhi",
Cars: ["Ford", "BMW", "Fiat"],
},
{
Name: "Cardekho",
Location: "Mumbai",
Cars: ["Ford", "Fiat"],
},
{
Name: "Tata Arena",
Location: "Pune",
Cars: ["Ford", "BMW"],
},
{
Name: "Nexa Showroom",
Location: "Noida",
Cars: ["Suzuki", "Ford", "Tata", "Fiat"],
},
];
console.log(combine(showrooms, filterArray));

最新更新