嘿,我想問一下是否可以在 10 個如下所示的 JSON 檔案中找到可以說的內容:
1.json:
{
"name": "Miami",
"attributes": [
{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
}
2.json:
{
"name": "New York",
"attributes": [
{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
等等......只是不同的屬性值。假設我只想找到 People > 2500 的城鎮,我什至不確定是否有可能,或者我是否需要將 json 檔案上傳到某個資料庫?
謝謝你。
uj5u.com熱心網友回復:
const data = [{
"name": "Miami",
"attributes": [{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
},
{
"name": "New York",
"attributes": [{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
]
const result = data
// find cities with attribute `People` with value greater than 2500
.filter(d => d.attributes.find(attr => attr.att_type === 'People').value > 2500)
// get name of the city
.map(d => d.name)
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/513503.html
