Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove an array item in TypeScript?

I have an array that I've created in TypeScript and it has a property that I use as a key. If I have that key, how can I remove an item from it?

like image 653
Tim Almond Avatar asked Mar 08 '13 10:03

Tim Almond


2 Answers

Same way as you would in JavaScript.

delete myArray[key]; 

Note that this sets the element to undefined.

Better to use the Array.prototype.splice function:

const index = myArray.indexOf(key, 0); if (index > -1) {    myArray.splice(index, 1); } 
like image 177
blorkfish Avatar answered Sep 22 '22 13:09

blorkfish


let foo_object; // Itemitem(object here) to remove this.foo_objects = this.foo_objects.filter(obj => return obj !== foo_object); 
like image 22
Malik Shahzad Avatar answered Sep 22 '22 13:09

Malik Shahzad