Languages
[Edit]
EN

TypeScript - remove specific array item by index

0 points
Created by:
cory
1786

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

Quick solution:

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

 

Practical example

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


// Usage example:

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

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

Output:

[ 'a', 'b', 'd' ]
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