How to combine an array in javascript

前端 未结 3 1057
死守一世寂寞
死守一世寂寞 2020-11-22 06:56

Hello I want to merge a array based on the unique item in the array.

The object that I have

totalCells = []

In this totalCells arr

3条回答
  •  一生所求
    2020-11-22 07:31

    What about something like this :

    totalCells.reduce(function(a, b) {
      if(!a[b.lineNumber]){
        a[b.lineNumber] = {
          lineNumber: b.lineNumber,
          cells: [b.cellwidth]
        }
      }
      else{
        a[b.lineNumber].cells.push(b.cellwidth);
      }
      return a;
    }, []);
    

    Hope this helps!

提交回复
热议问题