Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array of the values of all properties of objects inside an array of objects?

I want to get an array of the values of each object I have.

I have this:

const numDataPoints = [
    {
      x0: {x: 807, y: 625},
      x1: {x: 15, y: 20},
      x2: {x: 5, y: 20}
    },
    {
      x0: {x: 11, y: 6},
      x1: {x: 16, y: 21},
      x2: {x: 7, y: 22}
    }
  ];

I want this:

[
  [807, 625],
  [15, 20],
  [5, 20],
  [11, 6],
  [16, 21],
  [7, 22]
]

I tried this:

numDataPoints.map((array) => array.map(axis => [axis.x, axis.y]));

but it throws this error:

Uncaught TypeError: array.map is not a function

like image 905
Meta Code Avatar asked Dec 02 '22 11:12

Meta Code


1 Answers

You can use map method with Object.values and spread syntax ....

const data =  [{ x0: {x: 807, y: 625}, x1: {x: 15, y: 20}, x2: {x: 5, y: 20} }, { x0: {x: 11, y: 6}, x1: {x: 16, y: 21}, x2: {x:7, y: 22} }];

const result = [].concat(...data.map(Object.values)).map(Object.values)
console.log(result)
like image 180
Nenad Vracar Avatar answered Dec 21 '22 00:12

Nenad Vracar