如何在javascript中将带有JSON对象的二维数组转换为单个数组?

时间:2016-01-12 18:57:57

标签: javascript arrays json multidimensional-array

这里我在数组中有一个JSON对象,并将其推送到employeeArray,即

employeeArray =[  
  [  
    {  
      "ID":"967",
      "NAME":"Dang, Lance D",
      "Email":"Lance.Dang@xyz.com"
    }
  ],
  [  
    {  
      "ID":"450",
      "NAME":"Starnes, Mitch",
      "Email":"Mitchell.Starnes@xyz.com"
    }
  ],
  [  
    {  
      "ID":"499",
      "NAME":"Cosby, Lance H",
      "Email":"Lance.Cosby@xyz.com"
    }
  ]
]; 

如何将这个带到像这样的JSON对象的单个数组中

employeeArray =[  
  {  
    "ID":"967",
    "NAME":"Dang, Lance D",
    "Email":"Lance.Dang@xyz.com"
  },
  {  
    "ID":"450",
    "NAME":"Starnes, Mitch",
    "Email":"Mitchell.Starnes@xyz.com"
  },
  {  
    "ID":"499",
    "NAME":"Cosby, Lance H",
    "Email":"Lance.Cosby@xyz.com"
  }
];

帮助我如何使用上面的二维数组值并在纯javascript中构建我期望的数组

1 个答案:

答案 0 :(得分:3)

您希望展平数组,可以通过reduce调用来完成:

employeeArray.reduce((p, c) => p.concat(c), [])

ES5:

employeeArray.reduce(function (p, c) {
  return p.concat(c);
}, []);