group array of objects by id

后端 未结 8 535
抹茶落季
抹茶落季 2021-01-16 08:07

I am trying to loop through a array ob objects and group the items of the array into new arrays that have matching id:

API example:

    api_array [
          


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 08:28

    https://jsfiddle.net/u4k16ojz/5/

    var result = new Array(4);
    api_array.forEach(function(item, index){
      if (!result[item.id]){
        result[item.id] = [];
      }
      result[item.id].push(item);
    })
    

提交回复
热议问题