constuniqueItemsArray=['Apple','Banana','Orange','Banana','Apple'];// Adding new items to the ArrayuniqueItemsArray.push('Grapes');uniqueItemsArray.push('Orange');// Already exists but added again// Checking if a value exists in the ArrayconsthasGrapes=uniqueItemsArray.includes('Grapes');// Deleting an item from the ArrayuniqueItemsArray=uniqueItemsArray.filter(item=>item!=='Banana');uniqueItemsArray.forEach(item=>{console.log(item);});
✅ Use instead:
constuniqueItemsSet=newSet(['Apple','Banana','Orange','Banana','Apple']);// will show ['Apple', 'Banana', 'Orange']// Adding new unique items to the SetuniqueItemsSet.add('Grapes');uniqueItemsSet.add('Orange');// Checking if a value exists in the SetconsthasGrapes=uniqueItemsSet.has('Grapes');// Deleting an item from the SetuniqueItemsSet.delete('Banana');// Iterating through the Setfor (letitemofuniqueItemsSet){console.log(item);}
Top comments (16)