0
点赞
收藏
分享

微信扫一扫

【Camera2 教程二】Camera2相机打开和关闭接口调用详细说明

西特张 2天前 阅读 1

引入

es6数组有很多方法:map,find,filter返回一个新数组,every,some,Array.includes()返回的是boolean,

findIndex(),indexOf(),返回的是第一次出现的索引值,includes(),

forEach,不需要

数组合并 concact  [...arr]

unshhift push ,shift,pop

sort,reduce,splice,slice,reverse

返回的是新数组

1.Map

map必须要return 不然就会undefined。箭头函数如果只有一行的时候return可以不写。map经常会二次循环使用。里面的find也要return,但是由于箭头函数只有一行,所以可以不写return。

  this.tableData = this.tableData.map((item) => {
        const deptName = this.findLabelById(item.deptId, this.deptOptions);
        const userIds = item.userIds?.split(",");
        if (userIds) {
          const userNames = userIds
            .map((item2) => {
              const user = this.userList.find(
                (item3) => item3.userId === item2
              );
              // console.log("userNames", userNames);
              return user ? user.nickName : null;
            })
            .join(",");
          return { ...item, deptName, userNames };
        } else {
          return { ...item, deptName };
        }
      });

2.find

   find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined 。也是要return的!这个是简单的例子,难的是看我上面map里面的那个,其实是一样,但是我以为是个值,其实确切一点说返回的是:当你的数组是大数组,里面都是小数组,假设找到userid相同的那么他返回的就是拥有第一个useid的一整项。

const array1 = [5, 12, 8, 130, 44];

const found = array1.find((element) => element > 10);

console.log(found);
// Expected output: 12

3.filter过滤

过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。 记得return,下面代码就是过滤他们No不相等的全部返回。

  const filterData = this.originalData.filter((item) => {
        return item.No !== No;
      });

 这个难一点,这个是判断unique是否存在这个属性,如果存在就不要返回,如果不存在就返回,这个是和对象键值有关知识点。

 uniqueItems() {
      const unique = {};
      this.remindersList = this.workList.filter((item) => {
        if (!unique[item.labelName]) {
          unique[item.labelName] = true;
          return true;
        }
        return false;
      });
    },
 //         const obj = {
        //             a: 1,
        //             b: 2,
        //             c: []
        //           }
        //           obj['c']就是数组,拿到的是obj.c的值,可以用push

返回的是布尔值

1.every 

判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true 也是要return

2.some

判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。也是要return

const arr = [1, 2, 3, 4, 5];
const arr2 = arr.some(function(x) {
return x < 3;
}); 
console.log(arr2); //true
const arr3 = arr.some(function(x) {
return x < 1;
}); 
console.log(arr3); // false

3.includes()

includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false

const array1 = [1, 2, 3];

console.log(array1.includes(2));
// Expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// Expected output: true

console.log(pets.includes('at'));

 返回的是索引值

1.findIndex()

      findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。


      // findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。
      const existingDataIndex = this.tableData.findIndex(
        (item) => item.workContent === value.workContent
      );
      // console.log("existingDataIndex", existingDataIndex);
      if (existingDataIndex !== -1) {
        // 如果存在相同的 workContent,则追加数据
        this.$set(this.tableData, existingDataIndex, {
          workContent: value.workContent,
          workNorm:
            this.tableData[existingDataIndex].workNorm + ", " + value.workNorm,
          sort: value.sort,
        });
      } else {
        // 如果不存在相同的 workContent,则添加新数据
        this.tableData.push({
          workContent: value.workContent,
          workNorm: value.workNorm,
          sort: value.sort,
        });
      }

2.Array.indexOf()和 Array.lastIndexOf()

indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。

indexOf():接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。其中, 从数组的开头(位置 0)开始向后查找。
lastIndexOf:接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。其中, 从数组的末尾开始向前查找。
这两个方法都返回要查找的项在数组中的位置,或者在没找到的情况下返回1。在比较第一个参数与数组中的每一项时,会使用全等操作符。

const arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5)); //2
console.log(arr.lastIndexOf(5)); //5
console.log(arr.indexOf(5,2)); //2
console.log(arr.lastIndexOf(5,4)); //2
console.log(arr.indexOf("5")); //-1

 

找到匹配项后,findIndex和indexOf这两个方法都不再继续搜索 

栈方法

pop,push

  • push(): 可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度。
  • pop():数组末尾移除最后一项,减少数组的 length 值,然后返回移除的项。

 队列方法

unshift,shift 

  • shift():删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined 。
  • unshift:将参数添加到原数组开头,并返回数组的长度 。
  • 这组方法和上面的push()和pop()方法正好对应,一个是操作数组的开头,一个是操作数组的结尾。

归并方法 

Array.reduce()和 Array.reduceRight() 

这两个方法都会实现迭代数组的所有项,然后构建一个最终返回的值。reduce()方法从数组的第一项开始,逐个遍历到最后。而 reduceRight() 则从数组的最后一项开始,向前遍历到第一项。

这两个方法都接收两个参数:

每一项上调用的函数
(可选的)作为归并基础的初始值
传给 reduce()和 reduceRight() 的函数接收 4 个参数:前一个值、当前值、项的索引和数组对象。这个函数返回的任何值都会作为第一个参数自动传给下一项。第一次迭代发生在数组的第二项上,因此第一个参数是数组的第一项,第二个参数就是数组的第二项。

下面代码用 reduce() 实现数组求和,数组一开始加了一个初始值10。

const values = [1,2,3,4,5];
const sum = values.reduceRight(function(prev, cur, index, array){
return prev + cur;
},10);
console.log(sum); //25

排序方法 

reverse()

反转数组项的顺序
let arr = [13, 24, 51, 3];
console.log(arr.reverse()); //[3, 51, 24, 13]
console.log(arr); //[3, 51, 24, 13](原数组改变)


sort()

按升序排列数组项——即最小的值位于最前面,最大的值排在最后面。
在排序时,sort() 方法会调用每个数组项的 toString() 转型方法,然后比较得到的字符串,以确定如何排序。即使数组中的每一项都是数值, sort() 方法比较的也是字符串,因此会出现以下的这种情况:
let arr1 = ["a", "d", "c", "b"];
console.log(arr1.sort()); // ["a", "b", "c", "d"]
let arr2 = [13, 24, 51, 3];
console.log(arr2.sort()); // [13, 24, 3, 51] 两位数字先比较第一位再比较第二位
console.log(arr2); // [13, 24, 3, 51](原数组被改变)

原数组未被改变

concat()  合并

将参数添加到原数组中。这个方法会先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。在没有给 concat() 方法传递参数的情况下,它只是复制当前数组并返回副本

const arr = [1,3,5,7];
const arrCopy = arr.concat(9,[11,13]);
console.log(arrCopy); //[1, 3, 5, 7, 9, 11, 13]
console.log(arr); // [1, 3, 5, 7](原数组未被修改)


从上面测试结果可以发现:传入的不是数组,则直接把参数添加到数组后面,如果传入的是数组,则将数组中的各个项添加到数组中。但是如果传入的是一个二维数组呢?

const arrCopy2 = arr.concat([9,[11,13]]);
console.log(arrCopy2); //[1, 3, 5, 7, 9, Array[2]]
console.log(arrCopy2[5]); //[11, 13]

上述代码中,arrCopy2 数组的第五项是一个包含两项的数组,也就是说concat方法只能将传入数组中的每一项添加到数组中,如果传入数组中有些项是数组,那么也会把这一数组项当作一项添加到 arrCopy2 中

slice()  返回从原数组中指定开始下标到结束下标之间的项组成的新数组

返回从原数组中指定开始下标到结束下标之间的项组成的新数组
slice() 方法可以接受一或两个参数,即要返回项的起始和结束位置。
在只有一个参数的情况下, slice() 方法返回从该参数指定位置开始到当前数组末尾的所有项
如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项 

const arr = [1,3,5,7,9,11];
const arrCopy = arr.slice(1);
const arrCopy2 = arr.slice(1,4);
const arrCopy3 = arr.slice(1,-2);
const arrCopy4 = arr.slice(-4,-1);
console.log(arr); //[1, 3, 5, 7, 9, 11](原数组没变)
console.log(arrCopy); //[3, 5, 7, 9, 11]
console.log(arrCopy2); //[3, 5, 7]
console.log(arrCopy3); //[3, 5, 7]
console.log(arrCopy4); //[5, 7, 9]
// arrCopy只设置了一个参数,也就是起始下标为1,所以返回的数组为下标1(包括下标1)开始到数组最后。 
// arrCopy2设置了两个参数,返回起始下标(包括1)开始到终止下标(不包括4)的子数组。 
// arrCopy3设置了两个参数,终止下标为负数,当出现负数时,将负数加上数组长度的值(6)来替换该位置的数,因此就是从1开始到4(不包括)的子数组。 
// arrCopy4中两个参数都是负数,所以都加上数组长度6转换成正数,因此相当于slice(2,5)。

删除、插入和替换

splice()

 splice():很强大的数组方法,它有很多种用法,可以实现删除、插入和替换。
删除:可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。例如, splice(0,2)会删除数组中的前两项。
插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。例如,splice(2,0,4,6)会从当前数组的位置 2 开始插入4和6。
替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。例如,splice (2,1,4,6)会删除当前数组位置 2 的项,然后再从位置 2 开始插入4和6。
splice()方法始终都会返回一个数组,该数组中包含从原始数组中删除的项,如果没有删除任何项,则返回一个空数组。

let arr = [1,3,5,7,9,11];
let arrRemoved = arr.splice(0,2);
console.log(arr); //[5, 7, 9, 11]
console.log(arrRemoved); //[1, 3]
let arrRemoved2 = arr.splice(2,0,4,6);
console.log(arr); // [5, 7, 4, 6, 9, 11]
console.log(arrRemoved2); // []
let arrRemoved3 = arr.splice(1,1,2,4);
console.log(arr); // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRemoved3); //[7]

filter也可以做删除哦

forEach用法

即不修改原数组 也可以修改原数组,不修改时相当于for循坏,

    [...sourceData].forEach((item) => {
        if (classifyData.includes(item.workContent) === -1) {
          classifyData.push(item.workContent);
        }
      });

修改的时候,下面两个例子

        item.workList[keys].forEach((items) => {
                  mindex++;
                  items.index = mindex;
                });
  let marr = JSON.parse(JSON.stringify(this.tableData));
      marr.forEach((item) => {
        item.userIds = "";
        item.userNames = "";
        item.deptName = "";
        item.deptId = "";
      });
      this.tableData = marr;

数组合并

一种:concat 看上面的concat

一种:展开运算符

 if (Array.isArray(value)) {
          newData = [...this.originalData, ...value];
        } else {
          // 若 res.data 不是数组,将其包装成数组再进行合并
          newData = [...this.originalData, value];
        }
举报

相关推荐

0 条评论