如何将嵌套数组对转换为数组中的对象

时间:2016-11-11 07:06:27

标签: javascript arrays object

我的任务是将以下数组转换为对象数组:

var arr = [
  [
    ['firstName', 'Joe'],
    ['lastName', 'Blow'],
    ['age', 42],
    ['role', 'clerk'],
    [
      ['firstName', 'Mary'],
      ['lastName', 'Jenkins'],
      ['age', 36],
      ['role', 'manager']
    ]
  ]
]

我需要将arr转换为如下所示的对象数组:

[
 {firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
 {firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]

6 个答案:

答案 0 :(得分:3)

程序化解决方案(也可在https://jsfiddle.net/edorka/jnLxyzhb/获得):

var arr = [[ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'] ]];

var empty = {};
var result = arr.map(function(objectArray){
    var object = this;
    var attribute = objectArray.map(function(attrArray){
        var name = attrArray[0], value = attrArray[1];
        object[name] = value;
        return object;
    }, object);
    return this;
}, empty);

console.log(result);

答案 1 :(得分:1)

你可以像这样转换它 -

var arr = [
            {'firstName':'Joe','lastName':'Blow','age':42,'role':'clert‌​'},
            {'firstName':'Mar‌​y','lastName':'Jenki‌​ns','age':36,'role':‌​'manager'}
          ]

答案 2 :(得分:0)

对象数组的示例

var arr = [
    {
       'firstName' : 'Joe',
       'lastName' : 'Blow',
       'age' : 42,
       'role' : 'clerk'
    },
    {
       'firstName' : 'Mary',
       'lastName' : 'Jenkins',
       'age' : 36,
       'role' : 'manager'
    } 
];

答案 3 :(得分:0)

您可以对嵌套数组使用迭代和递归方法。

var array = [[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]]],
    result = [];

array[0].forEach(function iter(a, i) {
    i || result.push({});
    if (i < 4) {
        result[result.length - 1][a[0]] = a[1];
        return;
    }
    Array.isArray(a) && a.forEach(iter);
});
   
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:0)

function convert(array) {
  return array.map(function(person) {
    return person.reduce(function(obj, prop, index) {
      if (!obj[prop[0]]) {
        obj[prop[0]] = prop[1];
      }

      return obj;
    }, {});
  }); 
}
  • 使用地图功能选择每个&#34; person&#34;阵列
  • 使用reduce函数转换每个&#34; person&#34;数组到对象
  • map函数也将返回一个新的对象数组
  • 原始数组不会被更改

答案 5 :(得分:-1)

如果你格式化你的代码,你会看到最初有一个结束括号']'在最后丢失。你的结构是这样的

var arr = [
    [
        ['firstName', 'Joe'],
        ['lastName', 'Blow'],
        ['age', 42],
        ['role', 'clerk'],
        [
            ['firstName', 'Mary'],
            ['lastName', 'Jenkins'],
            ['age', 36],
            ['role', 'manager']
        ]
    ]
]

我想你想做这样的事情

var arr = [
    [
        ['firstName', 'Joe'],
        ['lastName', 'Blow'],
        ['age', 42],
        ['role', 'clerk']
    ],
    [
        ['firstName', 'Mary'],
        ['lastName', 'Jenkins'],
        ['age', 36],
        ['role', 'manager']
    ]
];

但我会建议你这样做

var arr = [
    {
        'firstName': 'Joe',
        'lastName': 'Blow',
        'age': 42,
        'role': 'clerk'
    },
    {
        'firstName': 'Mary',
        'lastName': 'Jenkins',
        'age': 36,
        'role': 'manager'
    }
];