gpt4 book ai didi

javascript - 使用javascript过滤json数据数组

转载 作者:行者123 更新时间:2023-11-29 17:34:47 26 4
gpt4 key购买 nike

我有一个像下面这样的 json 数据

 var data =  [
{
"type": "Feature",
"id": 1,
"properties":
{
"name": "William",
"categorie": 107,
"laporan":"Fire",
"time":1,
"type": "Firefighter",
"phone_number": "0111111"
}, "geometry":
{
"type": "Point",
"coordinates": [ 106.814893, -6.219156]
}
}
,
{
"type": "Feature",
"id": 7,
"properties":
{
"name": "John",
"categorie": 103,
"laporan":"Thief",
"time":3,
"type": "Police",
"phone_number": "0987654321"
}, "geometry":
{
"type": "Point",
"coordinates": [ 106.794107, -6.286935 ]
}
}
]

我想根据时间过滤上面的json数据来获取propertiescoordinates,我试过使用下面的代码,但它没有显示我想要的数据

  var data_filter=data.filter(function(item){
return item.properties.time==1;
});
console.log(data_filter)

你知道我是如何过滤 json 数据来获取属性和坐标数据的吗?

谢谢

最佳答案

您终于可以映射想要的属性了。

var data = [{ type: "Feature", id: 1, properties: { name: "William", categorie: 107, laporan: "Fire", time: 1, type: "Firefighter", phone_number: "0111111" }, geometry: { type: "Point", coordinates: [106.814893, -6.219156] } }, { type: "Feature", id: 7, properties: { name: "John", categorie: 103, laporan: "Thief", time: 3, type: "Police", phone_number: "0987654321" }, geometry: { type: "Point", coordinates: [106.794107, -6.286935] } }],
result = data
.filter(item => item.properties.time == 1)
.map(({ properties, geometry: { coordinates } }) => ({ properties, coordinates }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用javascript过滤json数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57883641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com