ES6数组中删除指定元素

知识点:

ES6从数组中删除指定元素

findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。
 arr.splice(arr.findIndex(item => item.id === data.id), 1)

http://louiszhai.github.io/2017/04/28/array/

1:js中的splice方法

splice(index,len,[item]) 注释:该方法会改变原始数组。

splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值

index:数组开始下标 len: 替换/删除的长度 item:替换的值,删除操作的话 item为空

如:arr = [‘a‘,‘b‘,‘c‘,‘d‘]

删除 ---- item不设置

arr.splice(1,1) //[‘a‘,‘c‘,‘d‘] 删除起始下标为1,长度为1的一个值,len设置的1,如果为0,则数组不变

arr.splice(1,2) //[‘a‘,‘d‘] 删除起始下标为1,长度为2的一个值,len设置的2

替换 ---- item为替换的值

arr.splice(1,1,‘ttt‘) //[‘a‘,‘ttt‘,‘c‘,‘d‘] 替换起始下标为1,长度为1的一个值为‘ttt’,len设置的1

arr.splice(1,2,‘ttt‘) //[‘a‘,‘ttt‘,‘d‘] 替换起始下标为1,长度为2的两个值为‘ttt’,len设置的1

添加 ---- len设置为0,item为添加的值

arr.splice(1,0,‘ttt‘) //[‘a‘,‘ttt‘,‘b‘,‘c‘,‘d‘] 表示在下标为1处添加一项‘ttt’

想要的:定义一个数组ARR,保存已勾选上的数据ID,改变勾选框的状态时从数组中删除或添加数据ID,点击批量删除时,获取数组ARR

操作:

1.页面设置

// 全选
         <th nz-th nzCheckbox>
            <label nz-checkbox [(ngModel)]="_allChecked" [nzIndeterminate]="_indeterminate" (ngModelChange)="_checkAll($event)">
            </label>
          </th>

//单选

<td nz-td nzCheckbox>
     <label nz-checkbox [(ngModel)]="data.checked" (ngModelChange)="_refreshStatus($event)" (click)="onSelect(data)">
     </label>
 </td>

2.全选操作触发事件

// 初始化全选和全部取消的状态
  _allChecked = false;
  _indeterminate = false;

   _bacthDelete = [];//定义一个数组,存放勾选的数据ID


// 点击全选触发的事件
  _checkAll(value) {
    if (value) {
      this._displayData.forEach(data => {
        data.checked = true;
        this._bacthDelete.push(data.id);
      },
      console.log(this._bacthDelete)
    );
    } else {
      this._displayData.forEach(data => {
        data.checked = false;
      },
      this._bacthDelete=[]
      );
         console.log(this._bacthDelete)
    }
    this._refreshStatus();
  }

3.点击单个数据状态

  onSelect(data: FormData): void {
    this.selectedTestId = data;
    console.log(data);
    if(!data.checked){
      let arr = this._bacthDelete;
      arr.splice(arr.findIndex(item => item.id === data.id), 1)
      console.log(arr)
    }else{
      this._bacthDelete.push(data.id);
       console.log( this._bacthDelete)
    }

  }

原文地址:https://www.cnblogs.com/linsx/p/8331623.html

时间: 2024-07-30 10:05:18

ES6数组中删除指定元素的相关文章

PHP 从数组中删除指定元素

<?php $arr1 = array(1,3, 5,7,8); $key = array_search(3, $arr1); if ($key !== false){ array_splice($arr1, $key, 1); } var_dump($arr1); // array(4) { [0]=> int(1) [1]=> int(5) [2]=> int(7) [3]=> int(8) } ?> <?php $arr2 = array(1,3, 5,7,

学underscore在数组中查找指定元素

前言 在开发中,我们经常会遇到在数组中查找指定元素的需求,可能大家觉得这个需求过于简单,然而如何优雅的去实现一个 findIndex 和 findLastIndex.indexOf 和 lastIndexOf 方法却是很少人去思考的.本文就带着大家一起参考着 underscore 去实现这些方法. 在实现前,先看看 ES6 的 findIndex 方法,让大家了解 findIndex 的使用方法. findIndex ES6 对数组新增了 findIndex 方法,它会返回数组中满足提供的函数的

在一个升序的但是经过循环移动的数组中查找指定元素

数组是升序的,数组经过循环移动之后,肯定是有左半部分或者有半部分还是升序的. 代码: public class SearchRotateArray { public static int search(int a[], int l, int u, int x) { while(l<=u){ int m = (l+u)/2; if(x==a[m]){ return m; }else if(a[l]<=a[m]){ //左半部分升序排列 if(x>a[m]){ l=m+1; }else if

旋转数组中查找指定元素

如题,在旋转数组中查找指定元素,考虑到多种情况,网上的方法大部分没有考虑,当low,high,mid三个值相等时的情况. 代码如下: int findAll(int A[],int low,int high,int value)//当三个相等时,查找全部元素的函数. { for(int i = low;i < high;i++) { if(A[i]==value) return i; } return -1; } int find(int A[],int n,int value)//查找旋转数组

[Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A =[1

从jarray中删除指定元素的问题

string jsonText = "[{\"a\": \"aaa\",\"b\": \"bbb\",\"c\": \"ccc\"},{\"a\": \"aa\",\"b\": \"bb\",\"c\": \"cc\"}]"; var mJObj =

[Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A =[1,1,1,2,2,3], Your function should return length =5, and A is now[1,1,2,2,3]. 题意:可以保留一个重复的元素. 思路:第一种是和Remove duplicates from sorte

javascript从数组中删除一个元素

Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; pubArray.remove(pubArray[i]);

php数组中删除元素之重新索引

如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊 <?php $arr = array('a','b','c','d'); unset($arr[1]); print_r($arr); ?> print_r($arr) 之后,结果却不是那样的,最终结果是 Array ( [0] => a [2] => c [3] => d ) 那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是 array_splice(): <?ph