对象数组的3维数组

时间:2017-08-26 05:20:42

标签: javascript arrays loops multidimensional-array

已经坚持了一段时间。

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

屈服:

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

如何改变这一点?我已经尝试过三重嵌套for循环,map / reduce和shift(),但我无法让它完全转换

6 个答案:

答案 0 :(得分:5)

试试这个解决方案。使用 Array#map 来迭代第一级项目。在map函数中,通过Array#forEach迭代嵌套数组项并填充您的对象。然后从map返回该对象。



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

const newEmp = employees.map(emp => {
   const obj = {};
   
   emp.forEach(([prop, value]) => obj[prop] = value);
   
   return obj;
});

console.log(newEmp);




答案 1 :(得分:4)

您可以使用Array#map循环外部/主阵列。

然后,您可以使用Array#reduce将主数组中的每个数组更改为object。



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

employees = employees.map(employee => employee.reduce((acc, item) => {
    acc[item[0]] = item[1];
    return acc;
}, {}));

console.log(employees);




答案 2 :(得分:3)

这就是我要做的事情:



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

const entriesToObject = entries => {
    const object = {};
    for (const [key, value] of entries)
        object[key] = value;
    return object;
};

const result = employees.map(entriesToObject);

console.log(result);




请注意result可以使用Object.entries转换回employees,如下所示:



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

const employees = result.map(Object.entries);

console.log(employees);




因此,entriesToObject函数非常有用,因为它是Object.entries的{​​{3}}。

答案 3 :(得分:2)

这似乎有效:



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

translated = []

for(let dataGroup of employees) {
    person = {};

    for(let dataPoint of dataGroup) {
        person[dataPoint[0]] = dataPoint[1]
    }

    translated.push(person);
}

console.log(translated);




答案 4 :(得分:2)

您可以使用

的组合
  1. Array#map,对于完整的新对象和只有键/值对的对象,

  2. Object.assign,用于创建单个/值对对象作为参数的新对象,

  3. spread syntax ...,用于将数组的每个元素作为参数,

  4. destructuring assignment,用于获取数组中的键和值

  5. computed property names,用于将动态密钥转换为对象字面值。

  6. employees.map(a => Object.assign(...a.map(([k, v]) => ({ [k]: v }))))
    //        ^^^                         ^^^                              1 Array#map
    //                 ^^^^^^^^^^^^^                                       2 Object.assign
    //                               ^^^                                   3 spread syntax
    //                                         ^^^^^^                      4 destructuring
    //                                                       ^^^           5 computed prop
    

    
    
    let employees = [[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]];
        result = employees.map(a => Object.assign(...a.map(([k, v]) => ({ [k]: v }))));
        
    console.log(result);
    
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    
    
    

答案 5 :(得分:0)

对于不同的方法,使用Array#reduceArray#forEach函数



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

var res = employees.reduce((a, b) => {
  var obj = {};
  b.forEach(function(i) {
    obj[i[0]] = i[1];
  })
  a.push(obj);
  return a;
}, [])

console.log(res)