js set delete item by index code example

Example 1: array remove index from array

const array = [2, 5, 9];

//Get index of the number 5
const index = array.indexOf(5);
//Only splice if the index exists
if (index > -1) {
  //Splice the array
  array.splice(index, 1);
}

//array = [2, 9]
console.log(array);

Example 2: remove from set javascript

// Create new set
const animals = new Set();

// Add to set
animals.add('cat');
animals.add('dog');

// Remove from set
animals.delete('cat'); // returns true
animals.delete('dog'); // returns true
animals.delete('horse'); // returns false

Tags:

Misc Example