How to React is not to render items with the same field?

How to React is not to render items with the same field? That is, there is an array of objects that it should display, but the elements with the same field, for example id (I understand that this can not be, but as an example), print only once and the remaining elements have the same id to ignore and display on map om the other elements which is not repeated id
April 19th 20 at 12:22
3 answers
April 19th 20 at 12:24
Solution
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;
});
cool, thanks - don_Miller62 commented on April 19th 20 at 12:27
April 19th 20 at 12:26
no, you first have to filter the array
in haste so if there's not a billion elements, the ride and performance

ar = [{id: 2},{id: 23},{id: 21},{id: 244},{id: 21},{id: 2},{id: 22},{id: 23}]

const uniqIds = new Set(ar.map(a => a.id));

[...uniqIds]
 .map(id => ar.find(a => id === a.id))
 .map(el => <Component data={el} />)
You understand that you have a quadratic complexity? - rafael_Denesik commented on April 19th 20 at 12:29
@rafael_Denesik, after another option in the comments, Yes, but I say "it" ^^ - Abbigail.Rohan commented on April 19th 20 at 12:32
April 19th 20 at 12:28
Need to submit from array with unique elements, why the extra data to send?
too true - don_Miller62 commented on April 19th 20 at 12:31

Find more questions by tags ReactJavaScript