[英]How to filter JSON with nested object
我有一個帶有嵌套對象的 JSON。 它看起來像這樣:
var cars = [
{
"id":1,
"name":"Ford Focus",
"attributes":{
"color":"gray",
"speed":"200"
}
},
{
"id":2,
"name":"Ford Fiesta",
"attributes":{
"color":"red",
"speed":"180"
}
},
];
我想按嵌套對象中的值進行過濾(例如所有帶有綠色的汽車)。
使用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'));
簡單的答案使用lodash
https://lodash.com/docs/4.17.13#filter
或者
你可以使用這個簡單的片段
var redCars = cars.filter(car=>car.attributes.color==='red');
console.log(redCars);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.