0

I have this javascript object children with multiple array inside and the array names are random.

enter image description here

how can i loop through it without specifying 'S801', 'S901' etc so i can get some result like

[52.4655012, 13.2595648], [21.1006158, 79.0074763] , ......

Aaron
  • 33
  • 3
  • 1
    Please do not post images of code? Instead, provide a text-based [mcve]. -- Also take a look at [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries), [`Object.values`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/values), and [`Object.keys`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) and use the most appropriate one? – evolutionxbox Jan 19 '22 at 13:21
  • 1
    You can loop over objects with [for ... in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) – Reyno Jan 19 '22 at 13:23
  • 2
    Try doing `for (const key in myobject.children) { console.log(myobject.children[key]) } ` – AlexSp3 Jan 19 '22 at 13:32
  • Thanks @AlexSp3 your method works too – Aaron Jan 19 '22 at 13:45

1 Answers1

4

Use Object.values to get the values in the Object and then map over it.

const obj = {
  children: {
    S801: [52, 13],
    S901: [21, 79]
  }
};

Object.values(obj.children).map(([value1,value2]) => {
  console.log(value1,value2)
})
Shan
  • 1,468
  • 2
  • 12