转换对象数组数组的数组 [英] Converting Array of Objects into Array of Arrays

查看:105
本文介绍了转换对象数组数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有就是我需要的对象的数组转换成数组的数组的一个条件。

例如: -

  ArrayTest中= ArrayTest中[此数组中10个对象]

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

现在我想的对象此Array转换成数组的数组。

P.S。如果我知道,对象的属性名称,然后我可以将其转换。但我想做的事情
     动态。

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

  VAR outputData = [];
对于(VAR I = 0; I< inputData.length;我++){
    VAR输入= inputData [I]
    outputData.push([input.firstName,input.lastName]);
}


解决方案

试试这个:

  VAR输出= input.map(函数(OBJ){
  返回Object.keys(OBJ)的.sort()。地图(功能(键){
    返回OBJ [关键]
  });
});

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.

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

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

解决方案

Try this:

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

这篇关于转换对象数组数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆