我有一个带有嵌套对象的JSON。看起来像这样:
var cars = [
{
"id":1,
"name":"Ford Focus",
"attributes":{
"color":"gray",
"speed":"200"
}
},
{
"id":2,
"name":"Ford Fiesta",
"attributes":{
"color":"red",
"speed":"180"
}
},
];
我想按嵌套对象中的值进行过滤(例如,所有带有绿色的汽车)。
答案 0 :(得分:-1)
使用filter()
方法
var cars = [
{
"id":1,
"name":"Ford Focus",
"attributes":{
"color":"gray",
"speed":"200"
}
},
{
"id":2,
"name":"Ford Fiesta",
"attributes":{
"color":"red",
"speed":"180"
}
},
];
console.log(cars.filter(car => car.attributes.color === 'red'));
答案 1 :(得分:-1)
使用简单答案lodash
https://lodash.com/docs/4.17.13#filter
或
您可以使用这个简单的代码段
var redCars = cars.filter(car=>car.attributes.color==='red');
console.log(redCars);