我有一个在TypeScript中创建的数组,它有一个我用作键的属性。如果我有那个钥匙,我怎么才能从里面删除一个项目呢?
发布于 2013-03-08 22:06:56
与在JavaScript中使用的方法相同。
delete myArray[key];
请注意,这会将元素设置为undefined
。
最好使用Array.prototype.splice
函数:
const index = myArray.indexOf(key, 0);
if (index > -1) {
myArray.splice(index, 1);
}
发布于 2017-06-06 21:11:34
如果数组是对象类型,那么最简单的方法是
let foo_object // Item to remove
this.foo_objects = this.foo_objects.filter(obj => obj !== foo_object);
发布于 2017-11-20 03:40:07
对于ES6,您可以使用以下代码:
removeDocument(doc){
this.documents.forEach( (item, index) => {
if(item === doc) this.documents.splice(index,1);
});
}
https://stackoverflow.com/questions/15292278
复制相似问题