Languages
[Edit]
EN

JavaScript - remove specific array item by index

3 points
Created by:
Greg
6230

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 
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