我有一个嵌套对象,其结构如下:
{
"sensors": [{
"probe": "PROBENAME",
"sensor": "SENSORNAME",
"status": "STATUS"
}, {
"probe": "PROBENAME",
"sensor": "SENSORNAME",
"status": "STATUS"
}]
}
有没有一种很好的方法来实际迭代这个方式,我只得到"探测"和#34;传感器。"每个都有1000个,我希望能够抓住所有这些。出于某种原因,每当我遍历结构时,我只是在返回中得到[object] [object]。
答案 0 :(得分:2)
此?或者我错过了什么?
yourObj.sensors.forEach(function(item){
console.log(item.probe, item.sensor);
});
答案 1 :(得分:0)
newObj = {sensors: obj.sensors.map(({sensor, probe} => ({sensor, probe}))};
答案 2 :(得分:0)
var input = {
"sensors": [{
"probe": "PROBENAME",
"sensor": "SENSORNAME",
"status": "STATUS"
}, {
"probe": "PROBENAME",
"sensor": "SENSORNAME",
"status": "STATUS"
}]
}
var output = []
input.sensors.forEach(function(sensor){
output.push({
probes : sensor.probe,
sensor : sensor.sensor
})
});
console.log(output)