javascript 将对象数组转换为数组数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22477612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:14:02  来源:igfitidea点击:

Converting Array of Objects into Array of Arrays

javascriptarraysobject

提问by KiddoDeveloper

There is a condition where i need to convert Array of objects into Array of Arrays.

有一种情况,我需要将对象数组转换为数组数组。

Example :-

例子 :-

arrayTest = arrayTest[10 objects inside this array]

single object has multiple properties which I add dynamically so I don't know the property name.

单个对象有多个我动态添加的属性,所以我不知道属性名称。

Now I want to convert this Array of objects into Array of Arrays.

现在我想将此对象数组转换为数组数组。

P.S. If I know the property name of object then I am able to convert it. But i want to do dynamically.

PS如果我知道对象的属性名称,那么我就可以转换它。但我想动态地做。

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

示例(如果我知道属性名称(firstName 和 lastName 是属性名称))

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

回答by sabof

Try this:

试试这个:

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

回答by Alex

Converts Array of objects into Array of Arrays:

将对象数组转换为数组数组:

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

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

回答by matewka

Use the for-inloop

使用for-in循环

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