I am currently working with a dynamic JavaScript object array that has varying structures. For example:
var someJsonArray = [
{point: 100, level: 3},
{point: 100, level: 3},
{point: 300, level: 6}
];
At times, it may have a different combination like:
var someJsonArray = [
{discussion: 5, level: 3},
{discussion: 4, level: 3},
{discussion: 3, level: 6}
];
The goal is to extract a specific field from each object and create an array containing the values. For instance, from the first array, the result should be [[100, 3],[100, 3], [300, 6]]
.
If the array had static properties, I could easily loop through and push them into one array like this:
var arr = [];
for(var i=0; i<someJsonArray.length; i++) {
arr.push(someJsonArray[i].id, someJsonArray[i].name);
}
console.log(arr)
However, since the array's structure is dynamic and can have any combination of properties (always two), I'm wondering how to achieve the same result. Any suggestions or workarounds for this challenge?