如何在json中获取重复对象的值,例如:
{ TableName: 'Table_0',
Rows:
[ [ 277, '2017-08-22T14:00:00Z' ],
[ 263, '2017-08-22T15:00:00Z' ],
[ 265, '2017-08-22T16:00:00Z' ],
[ 266, '2017-08-22T17:00:00Z' ],
[ 265, '2017-08-22T18:00:00Z' ],
[ 264, '2017-08-22T19:00:00Z' ] ] }
{ TableName: 'Table_1',
Rows: [ [ '{"Visualization":"table","Title":"","Accumulate":false,"IsQuerySorted":false,"Kind":"","Annotation":"","By":null}' ] ] }
我只想要第一个 Rows 对象的值,我该如何实现?
可以找到完整的json here
答案 0 :(得分:1)
您可以使用var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var data = new ol.Collection();
data.push(new ol.Feature({
geometry: new ol.geom.Point([0, 0])
}));
var f = new ol.Feature({
geometry: new ol.geom.Point([0, 0])
});
f.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: [255, 255, 255, 0.5]
}),
zIndex: Infinity
}),
}));
data.push(f);
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
features: data
})
});
var modify = new ol.interaction.Modify({
features: data
});
var map = new ol.Map({
interactions: ol.interaction.defaults().extend([modify]),
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 12
})
});
获取目标对象(基于Array.find
值):
TableName

如果你不支持ES6:
const data = { Tables:
[{ TableName: 'Table_0',
Rows:
[ [ 277, '2017-08-22T14:00:00Z' ],
[ 263, '2017-08-22T15:00:00Z' ],
[ 265, '2017-08-22T16:00:00Z' ],
[ 266, '2017-08-22T17:00:00Z' ],
[ 265, '2017-08-22T18:00:00Z' ],
[ 264, '2017-08-22T19:00:00Z' ] ] },
{ TableName: 'Table_1',
Rows: [ [ '{"Visualization":"table","Title":"","Accumulate":false,"IsQuerySorted":false,"Kind":"","Annotation":"","By":null}' ] ] }]};
console.log(data.Tables.find(row => row.TableName === 'Table_0').Rows)
如果您知道索引,也可以使用简单数组访问:
var targetObject = data.Tables.find(function(row) {
return row.TableName === 'Table_0'
})
console.log(targetObject.Rows)