Javascript 从数组中删除指定项
我知道我可以从数组中删除最后一项和第一项,但我需要删除指定的基于项的值 这是我的数组Javascript 从数组中删除指定项,javascript,angular,Javascript,Angular,我知道我可以从数组中删除最后一项和第一项,但我需要删除指定的基于项的值 这是我的数组 arrayItems: { volume: number; title: string; }[]; this.arrayItems = [{ volume: 11, title: 'Title' }]; 这是我的例子,我有html <div *ngFor="let arrayItem of arrayItems; let i=index"&g
arrayItems: {
volume: number;
title: string;
}[];
this.arrayItems = [{
volume: 11,
title: 'Title'
}];
这是我的例子,我有html
<div *ngFor="let arrayItem of arrayItems; let i=index">
<label>{{arrayItem.i}}</label>
<div (click)="removeItem(i)">remove item</div>
</div>
但要删除此项不起作用,您知道这是错误的吗?这是因为您使用的是Array.slice,而不是Array.splice。SLice不会更改原始数组,但会返回一个新数组,但是splice也会修改原始数组以供参考,请参见使用splice从JSON数组中删除指定项,如下所示:
removeItem(index) {
this.arrayItems.splice(index, 1);
}
使用拼接而不是切片。
removeItem(index) {
this.arrayItems.splice(index, 1);
}