How can I make two dimensional array with JSON objects into single array in javascript?

非 Y 不嫁゛ 提交于 2020-01-06 19:38:30

问题


Here i have a JSON object in an array and have it pushed to employeeArray which is

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"
    }
  ]
]; 

How do i get this into a single array with JSON objects like this

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"
  }
];

help me how to use above two dimensional array values and build my expected array in pure javascript


回答1:


You want to flatten the array, which can be done with a reduce call:

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

ES5:

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


来源:https://stackoverflow.com/questions/34751702/how-can-i-make-two-dimensional-array-with-json-objects-into-single-array-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!