How to divide an array into multiple arrays according to the values of the objects in the array

the data I get is an array of multiple objects;
I want to divide this array into multiple arrays according to the value of one of the objects;
, for example, according to the value of productId in it, give the value of 1 to 2. 3. My idea is to first iterate through the if to determine the matching values and index values and store the array in the new array;
but I don"t know how to implement it, or if there is any other more concise way.

data is shown in the following figure:
clipboard.png

Mar.02,2021

use filter, if you only want to get one array. If you want to get more than one array, use reduce


var a = [{id: 1}, {id: 3}, {id: 2}, {id: 3}, {id: 1}];
var b = a.reduce((r, x) => ((r[x.id] || (r[x.id] = [])).push(x), r), {});
var c = Object.keys(b).map(x => b[x]);

console.log(c); // [[{id: 1}, {id: 1}], [{id: 2}], [{id: 3}, {id: 3}]]

const groupBy = (arr, fn) =>
  arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
    acc[val] = (acc[val] || []).concat(arr[i]);
    return acc;
  }, {});

Object.values(groupBy(arr, 'projectId'))

let result=array.reduce(function(initArray,item){
    let index=item.productid;
    if(initArray[index]){
        initArray[index].push(item)
    }else{
        initArray[index]=[item]
     }
    return  initArray;
},[])
console.log(result)
.
Menu