数组操作:数组去重、数据调换位置、数组元素删除、双数组去重

 // 模态框中的数组删除函数
    /* arr:当前数组
    *  val:需要删除的元素名
    */
    removeByValue(arr, val) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
          arr.splice(i, 1);
          break;
        }
      }
    },

// 合并两个数组,然后调换里面元素的位置
     console.log("数组1", UnusualArray);
        console.log("数组2", this.columnsIndexValue);
        this.columnes = this.columnsIndexValue.concat(UnusualArray);
        console.log("数组3", this.columnsIndexValue.concat(UnusualArray));
        let x = 4,
          y = this.columnes.length;
        this.columnes.splice(
          x - 1,
          1,
          ...this.columnes.splice(y - 1, 1, this.columnes[x - 1])
        );
        if (y > 5) {
          this.columnes.forEach(item => {
            if (item.title === "监测值") {
              item.width = 300;
            }
          });
        }
        console.log("数组4", this.columnes);
 /**
     * @description: 两个数组去重的方法,删除重复的数据,得到新的数组
     * @param {type} : array1数组1,array2数组2
     * @return: 返回去重后的数组
     */
    ArrayWeightRemoval(array1, array2) {
      let tempArray1 = [];
      let tempArray2 = [];
      for (let i = 0; i < array2.length; i++) {
        tempArray1[array2[i]] = true;
      }
      for (let i = 0; i < array1.length; i++) {
        if (!tempArray1[array1[i]]) {
          tempArray2.push(array1[i]);
        }
      }
      return tempArray2;
    },

 /**
     * @description:数组去重,删除这个数组里面的重复数据
     * @param {type}: 有重复数据的数组
     * @return:无重复数据的数组
     */
    uniq(array) {
      let temp = [];
      for (let i = 0; i < array.length; i++) {
        // 如果当前数组的第i项在当前数组中第一次出现的位置是i,才存入数组;否则代表是重复的
        if (array.indexOf(array[i]) == i) {
          temp.push(array[i]);
        }
      }
      return temp;
    },

你可能感兴趣的:(数组操作:数组去重、数据调换位置、数组元素删除、双数组去重)