T
T
TheBott2020-04-16 18:03:11
JavaScript
TheBott, 2020-04-16 18:03:11

How to not render elements with the same margin in React?

How to not render elements with the same margin in React? That is, there is an array of objects that needs to be displayed, but elements with the same field, for example id (I understand that this cannot be, but as an example), should be displayed only once, and the remaining elements that have the same id should be ignored and displayed further by map other elements that do not have the same id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Klimenko, 2020-04-16
@TheBott

const array = [
    {
        name: 'Jonh',
        age: 32
    }, {
        name: 'James',
        age: 33
    }, {
        name: 'Jacob',
        age: 33
    }
];

const key = 'age';
const values = new Set();

const filtredArray = array.filter(item => {
    const value = item[key];
    if (!values.has(value)) {
        values.add(value);
        return true;
    }
    return false;
});

S
Sergey, 2020-04-16
@KingstonKMS

It is necessary to submit an array immediately with unique elements, why send extra data?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question