有没有一种方法可以通过使用.forEach或.map代替for循环来解决此问题?

时间:2019-04-03 15:15:37

标签: javascript arrays object

我需要编写一个将数组中的数组元素转换为对象的函数。尽管我已经找到了一种使用for-loop解决问题的方法,但我只是想知道是否有更简洁的方法来使用forEach或{ {1}}

问题是...

map

我需要将上面的数组转换成这样的东西。

var array: [
  [
    ['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-loop

就像我上面提到的那样,如果还有另一种解决问题的方法,那将是很好的。

7 个答案:

答案 0 :(得分:5)

在不久的将来,也许您可​​以使用Object.fromEntries()。目前某些浏览器版本支持该功能:Browser Compatibility

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

console.log(arr.map(Object.fromEntries));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

答案 1 :(得分:2)

您可以通过映射属性来映射新对象,并将所有属性连接到单个对象。

In [21]: tf.keras.layers.Dense(16)(tf.keras.layers.Dense(8))
...
AttributeError: 'Dense' object has no attribute 'shape'
var data = [[['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']], [['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']]],
    result = data.map(a => Object.assign(...a.map(([key, value]) => ({ [key]: value }))));
    
console.log(result);

答案 2 :(得分:1)

使用map和reduce。

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

    var transformed = array.map(a=> a.reduce((c, p) => {c[p[0]] = p[1]; return c;},{}));
    console.log(transformed);

答案 3 :(得分:1)

您可以串联使用mapreduce(将每个元素映射到reduce函数的结果,reduce函数将每个元素转换为键/值对):

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

const result = array.map(e => 
  e.reduce((a, [k, v]) => ((a[k] = v) || 1) && a, {}))
;

console.log(result);

答案 4 :(得分:1)

要获得预期结果,请使用以下使用map和forEach的选项

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

console.log(array.map(val => {
  const result = {}
  val.forEach(v => {
    result[v[0]] = v[1]
  })
  return result
}))

codepen-https://codepen.io/nagasai/pen/ROWmEX

答案 5 :(得分:1)

是的,您可以一行完成。

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

const transformed = array.map(upperArr => upperArr.reduce((acc, itemArr) => { acc[itemArr[0]] = itemArr[1]; return acc;}, {}));
console.log(transformed);

答案 6 :(得分:1)

具有map和forEach的函数:

function transformEmployeeData(array) {  
  return array.map(toObject => {
    const obj = {};
    toObject.forEach(([key, value]) => {
      obj[key] = value;
    });
    return obj;
  });
};

var array = [
  [['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['role', 'manager']
  ]
];
    
function transformEmployeeData(array) {  
  return array.map(toObject => {
    const obj = {};
    toObject.forEach(([key, value]) => {
      obj[key] = value;
    });
    return obj;
  });
};

console.log(transformEmployeeData(array));