Convert an array of objects to an array of arrays

There is a condition when I need to convert an array of objects to an array of arrays.

Example: -

arrayTest = arrayTest[10 objects inside this array]

one object has several properties that I add dynamically, so I don’t know the name of the property.

Now I want to convert this array of objects to an array of arrays.

PS If I know the property name of an object, then I can convert it. But I want to do it dynamically.

Example (if I know the name of the property (firstName and lastName are the name of the property))

var outputData = [];
for(var i = 0; i < inputData.length; i++) {
    var input = inputData[i];
    outputData.push([input.firstName, input.lastName]);
}
+4
source share
3 answers

Try the following:

var output = input.map(function(obj) {
  return Object.keys(obj).sort().map(function(key) { 
    return obj[key];
  });
});
+4
source

Converts an array of objects to an array of arrays:

var outputData = inputData.map( Object.values );

+4
source

for-in

var outputData = [];
for (var i in singleObject) {
    // i is the property name
    outputData.push(singleObject[i]);
}
0

Source: https://habr.com/ru/post/1532284/


All Articles