An array contains elements with duplicate id
<div data-id='48444884'>MM</div>
<div data-id='11101100'>LL</div>
<div data-id='72277727'>TT</div>
<div data-id='72277727'>TT</div>
<div data-id='48444884'>MM</div>
<div data-id='11101100'>LL</div>
<div data-id='72277727'>TT</div>
Can someone please tell me the best way to hide the duplicates div
I tried to do it:
- set "display: none;" to hide all div elements
- create array with a unique id
- set "display: block" for each element in with unique IDs
I know how to create an array with unique IDs through a new Set().map method:
const uniqId = new Set([...document.querySelectorAll('[data-id]')].map(id => id.dataset.id));
or by arr.filter:
let ids = Array.from(document.querySelectorAll('[data-id]'), id => id.dataset.id);
let uniqeid = ids.filter((element, index) => {
return ids.indexOf(element) === index;
});
console.log('UNIQE ID:', uniqeid);
But I don't really understand how to change style or add class to each element in array through id
Can someone please explain to me the correct way to do this