Reputation: 2410
I have a dynamic JavaScript object array with the following structure:
var someJsonArray = [
{point: 100, level: 3},
{point: 100, level: 3},
{point: 300, level: 6}
];
and sometime it would be with some other combination like:
var someJsonArray = [
{discussion: 5, level: 3},
{discussion: 4, level: 3},
{discussion: 3, level: 6}
];
I want to extract a field from each object, and get an array containing the values, for example from the fist array would give array as [[100, 3],[100, 3], [300, 6]]
.
If there would be some static array I would have pushed that into one array using a loop like below:
var arr = [];
for(var i=0; i<someJsonArray.length; i++) {
arr.push(someJsonArray[i].id, someJsonArray[i].name);
}
console.log(arr)
But in my case the array is dynamic and there could be any combination of property, but there will be only two property in each combination.
How to get the array of value objects from the dynamic array? Any help or workaround for this?
Upvotes: 2
Views: 205
Reputation: 89384
You can use Object.values
:
var arr = [
{point: 100, level: 3},
{point: 100, level: 3},
{point: 300, level: 6}
];
const res = arr.map(Object.values);
console.log(res);
Upvotes: 2