How do I remove an array item in TypeScript?
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);}
If array is type of objects, then the simplest way is
let foo_object // Item to removethis.foo_objects = this.foo_objects.filter(obj => obj !== foo_object);
With ES6 you can use this code :
removeDocument(doc){ this.documents.forEach( (item, index) => { if(item === doc) this.documents.splice(index,1); });}