我在TypeScript中创建了一个数组,它有一个属性作为键。如果我拥有这个键,我该如何从数组中删除一个元素?
我在TypeScript中创建了一个数组,它有一个属性作为键。如果我拥有这个键,我该如何从数组中删除一个元素?
和在 JavaScript 中一样。
delete myArray[key];
请注意,这将把元素设置为undefined
。
最好使用Array.prototype.splice
函数:
const index = myArray.indexOf(key, 0);
if (index > -1) {
myArray.splice(index, 1);
}
let foo_object; // Itemitem(object here) to remove
this.foo_objects = this.foo_objects.filter(obj => return obj !== foo_object);
this.foo_objects = this.foo_objects.filter(obj => obj !== foo_object)[0];
。 - Shift 'n Tab使用ES6,您可以使用以下代码:
removeDocument(doc){
this.documents.forEach( (item, index) => {
if(item === doc) this.documents.splice(index,1);
});
}
这是我的解决方案:
onDelete(id: number) {
this.service.delete(id).then(() => {
let index = this.documents.findIndex(d => d.id === id); //find index in your array
this.documents.splice(index, 1);//remove element from array
});
event.stopPropagation();
}
令departments为一个数组。你想从这个数组中移除一个项目。
departments: string[] = [];
removeDepartment(name: string): void {
this.departments = this.departments.filter(item => item != name);
}
你可以使用数组的 splice
方法来删除元素。
例如,如果你有一个名为 arr
的数组,请使用以下方法:
arr.splice(2, 1);
这里,索引为2的元素将是起始点,参数2将确定要删除多少个元素。
如果您想删除名为arr
的数组的最后一个元素,则可以执行以下操作:
arr.splice(arr.length-1, 1);
这将返回删除了最后一个元素的arr。var arr = ["orange", "mango", "banana", "sugar", "tea"];
arr.splice(arr.length-1, 1)
console.log(arr); // return ["orange", "mango", "banana", "sugar"]
pop
方法而不是 splice
。 - Iván Pérez这对我有用。
你的数组:
DummyArray: any = [
{ "id": 1, "name": 'A' },
{ "id": 2, "name": 'B' },
{ "id": 3, "name": 'C' },
{ "id": 4, "name": 'D' }
]
功能:
remove() {
this.DummyArray = this.DummyArray.filter(item => item !== item);
}
注意:此函数将删除数组中的所有对象。如果您想从数组中删除特定的对象,请使用以下方法:remove(id) {
this.DummyArray = this.DummyArray.filter(item => item.id !== id);
}
delete this.items[this.items.findIndex(item => item.item_id == item_id)];
或者this.items = this.items.filter(item => item.item_id !== item.item_id);
Typescript/Javascript中有多种选项可用于从数组中删除元素。
Splice是最佳选择,因为:
以下是使用Splice函数基于对象数组中的某个字段删除对象的示例:
const persons = [
{
firstName :'John',
lastName :'Michel'
},
{
firstName :'William',
lastName :'Scott'
},
{
firstName :'Amanda',
lastName :'Tailor'
}
]
console.log('Before Deleting :'+JSON.stringify(persons));
console.log('Deleting William:');
persons.splice(persons.findIndex(item => item.firstName === 'William'),1);
console.log('After Deleting William'+JSON.stringify(persons));
如果您需要从数组中删除一个给定的对象,并且您希望确保以下事项,请使用此操作:
const objWithIdToRemove;
const objIndex = this.objectsArray.findIndex(obj => obj.id === objWithIdToRemove);
if (objIndex > -1) {
this.objectsArray.splice(objIndex, 1);
}
const
而不是 let
。 - Radu Linu
var index: number = myArray.indexOf(key, 0);
- CorayThanindexOf
返回一个数字? - Chrisindex
,其中一个地方(splice
)需要看到一个数字,否则会出错。目前编译器无法防止您在那里犯错误。 - Jochem Kuijpersvar index = myArray.findIndex(x => x.prop==key.prop);
来查找特定属性与关键字匹配的对象的索引。 - Francisco Cabraldelete myArr[2]
实际上删除了myArr
的属性2
,这也不同于myArr[2] = undefined
。这个故事的寓意是,只需使用splice
来完成此任务,因为它是一种安全的方式,可以获得所需的效果,而不会产生混淆的副作用。 - Elianora