Answer the question
In order to leave comments, you need to log in
How to make the n-th number of arrays from an array of objects by value in the object?
There is an array of objects, for example:
let h = [
{
name: 'Petya',
id: 1
},
{
name: 'Vasya',
id: 2
},
{
name: 'Kolya',
id: 3
},
{
name: 'Sveta',
id: 1
}
];
let h1 = [
{
name: 'Petya',
id: 1
},
{
name: 'Sveta',
id: 1
}
];
let h2 = [
{
name: 'Vasya',
id: 2
}
];
let h3 = [
{
name: 'Kolya',
id: 3
}
];
Answer the question
In order to leave comments, you need to log in
as a result, I want to get arrays h1, h2, h3
const grouped = arr.reduce((acc, n) => ((acc[n.id] ??= []).push(n), acc), {});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question