Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数组中删除指定项_Javascript_Angular - Fatal编程技术网

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);
}