Languages
[Edit]
EN

JavaScript - remove specific array item by index

3 points
Created by:
Shri
8550

In this short article, we would like to show how to remove an item by index from an array in JavaScript.

Quick solution:

array.splice(itemIndex, 1);  // removes item with index

 

Practical example

// ONLINE-RUNNER:browser;

const removeItem = (array, index) => {
  	const removedItems = array.splice(index, 1);
  	if (removedItems.length > 0) {
    	return removedItems[0];
    }
  	return undefined;
};

// Usage example:

//    index:    0    1    2    3
const array = ['a', 'b', 'c', 'd'];

removeItem(array, 2); // removed 'c' item that has index 2
console.log(array);   // a, b, d

References

  1. JavaScript - remove specific array item by value or reference 

Alternative titles

  1. JavaScript - remove specific item from array by index
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join