0

I have an array retrieved from a JSON file and I am using dataArray.forEach(function(e, idx, array) to loop every element and create a table.

I can simply call for each element with console.log(e.id) but how can I list properties with incremental numbers on them without specifying it or its exact the position in loop?

I need to end up with something like this console.log(e.dp_1.value); but instead of this number I wish to use idx. Is this possible?

I have heard there is getChildByName but not sure how to use it and if it fits.

{
  "devices": [{
      "id": 0,
      "sn": "#12BB56CC9",
      "dp_1": {
        "value": "65345",
        "multiplier": 0.001,
        "digits": 2
      },
      "dp_2": {
        "value": "55345",
        "multiplier": 0.001,
        "digits": 2
      }
    },
    // much more like those
  ]
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Coffeeye
  • 31
  • 6
  • 4
    Change your data structure so you don't have the problem of incremental property names. Use an array as intended instead. – Rory McCrossan Oct 11 '22 at 11:14
  • `e.id` is the same as `e["id"]` - so `e.dp_1.value` is the same as `e["dp_1"].value` and `"dp_1"` is the same as `"dp_"+idx`. – freedomn-m Oct 11 '22 at 12:57

0 Answers0