Answer the question
In order to leave comments, you need to log in
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
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;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question