如何从JSON通过迭代和JavaScript提取由它的属性名的特定对象?

问题描述 投票:0回答:1
var datefilter= getUniqueFeatures(array.features,"Year");
datefilter=JSON.stringify(datefilter);
datefilter=JSON.parse(datefilter);
for (var key in datefilter){
    console.log(key);

我能够通过独特的功能隔离开来。

然后我让他们为对象。

此代码承认我的数据源。然而,与此output.enter image description here提供

我想知道的基础上,突出Project_Year属性的输出。所以,因为我得到12返回的值,我希望它说2017,2016,2015等。这听起来很容易,但我不能在网上找到任何东西。

javascript loops geojson
1个回答
0
投票

您可以使用Array.prototype.map()和解构赋值从对象的数组得到特定属性。 JSON.parse()JSON.stringify()似乎没有必要。

let dateFilter = [{properties:{Project_Year:2014}}, {properties:{Project_Year:2015}}];

let res = dateFilter.map(({properties:{Project_Year:p}}) => p);

console.log(res);

// or if the property name is also expected
let res1 = dateFilter.map(({properties:{Project_Year}}) => ({Project_Year}));

console.log(res1);
© www.soinside.com 2019 - 2024. All rights reserved.