如何将二维数组的行推送到对象

时间:2015-06-18 11:23:53

标签: javascript arrays for-loop

我有以下数组:

var myArray = [
[47,22,11],
[28,5,1],
[22,11,11]
]

我需要以下结构:

  var myStructure = [
    { Pass: 47, Warning: 22,  Fail: 11},
    { Pass: 28, Warning:5,  Fail: 1},
    { Pass: 22, Warning:11, Fail: 11;},
];

我该怎么做

2 个答案:

答案 0 :(得分:3)

您可以使用.map

var myArray = [
  [47,22,11],
  [28,5,1],
  [22,11,11]
];


var myStructure = myArray.map(function (el) {
  return {
    Pass: el[0],
    Warning: el[1],
    Fail: el[2],
  }
});

console.log(myStructure);

答案 1 :(得分:2)

您可以使用Array .map() method

var myStructure = myArray.map(function(el) {
                    return { Pass : el[0], Warning : el[1], Fail : el[2] };
                  });