Array Split in JavaScript [closed]

You can do it by using reduce helper, like this:

const tasks =[
     {id:1,level:1},
     {id:1,level:2},
     {id:1,level:3},
     {id:2,level:1},
     {id:2,level:2},
     {id:3,level:1}];

const newTasks = tasks.reduce((acc, data)=> {
    const target = acc.find(subArr => subArr.find(item => item.id == data.id));
    target ? target.push(data) : acc.push([data]);
    return acc;
} , []);

console.log(newTasks)

Leave a Comment