D
D
dmitry20002021-06-28 14:40:48
JavaScript
dmitry2000, 2021-06-28 14:40:48

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
                }
            ];

I need to go through those elements that have the same id and filter, for example, by the age field, whether there is a value there or not. Those. at the output get, focusing on this array, 3 other arrays

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Epikhin, 2021-06-28
@dmitry2000

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 question

Ask a Question

731 491 924 answers to any question