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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With