Answer the question
In order to leave comments, you need to log in
How to iterate over those fields whose id matches in an array of objects?
For example, there is an array:
let h = [
{
name: 'Petya',
age: 25,
id: 1
},
{
name: 'Vasya',
age: 23,
id: 2
},
{
name: 'Kolya',
age: null,
id: 3
},
{
name: 'Sveta',
age: null,
id: 1
}
];
Answer the question
In order to leave comments, you need to log in
With reduce, go through your array and get an object as the output, the keys of which will be the id of your objects, and the values will be arrays with objects. Those. you get an object in which your user objects will be divided into arrays. Then filter each array as you need. Next, expand everything back into a flat array
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question