0

I have a nested object with structure as follows:

{
 "sensors": [{
     "probe": "PROBENAME",
     "sensor": "SENSORNAME",
     "status": "STATUS"
 }, {
     "probe": "PROBENAME",
     "sensor": "SENSORNAME",
     "status": "STATUS"
 }]
}

Is there a good way to actually iterate over this in such a way that I only get the "probes" and "sensors." There are 1000s of each, and I want to be able to grab all of them. For some reason, whenever I iterate over the structure, I just get [object][object] in the return.

1
  • This has nothing to do with either JSON or node.js. Commented Nov 16, 2016 at 20:52

3 Answers 3

2

This? Or am I missing something?

yourObj.sensors.forEach(function(item){
  console.log(item.probe, item.sensor);
});
Sign up to request clarification or add additional context in comments.

Comments

0
newObj = {sensors: obj.sensors.map(({sensor, probe} => ({sensor, probe}))};

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.