美文网首页
数组 讲解之 扩展

数组 讲解之 扩展

作者: zhang463291046 | 来源:发表于2020-08-25 15:44 被阅读0次

    以下内容是引用或者借鉴别人的,自己只是做个笔记,方便学习。理解错误的地方,欢迎评论。如有侵权,私聊我删除,未经允许,不准作为商业用途

    静态方法

    • from()将类似数组的对象(具有属性length)和可遍历的对象(具有属性Symbol.iterator)转为真正的数组
    let arrayLike = {
        '0': 'a',
        '1': 'b',
        '2': 'c',
        length: 3
    };
    // ES5的写法
    var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']
    // ES6的写法
    let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
    
    Array.from('hello')  // ['h', 'e', 'l', 'l', 'o']
    
    let namesSet = new Set(['a', 'b'])
    Array.from(namesSet) // ['a', 'b']
    
    Array.from([1, 2, 3])  // [1, 2, 3]
    
    Array.from({ length: 3 });  // [ undefined, undefined, undefined ]
    
    // 第二个参数,对每个元素进行处理
    Array.from(arrayLike, x => x * x);
    // 等同于
    Array.from(arrayLike).map(x => x * x);
    Array.from([1, 2, 3], (x) => x * x)  // [1, 4, 9]
    Array.from({ length: 2 }, () => 'jack')  // ['jack', 'jack']
    
    • of()用于将一组值,转换为数组
    Array.of(3, 11, 8) // [3,11,8]
    Array.of(3) // [3]
    Array.of(3).length // 1
    Array.of() // []
    Array.of(undefined) // [undefined]
    

    静态方法图解

    image.png

    实例方法

    • copyWithin()将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组
    // copyWithin(target, start = 0, end = this.length)
    [1, 2, 3, 4, 5].copyWithin(0, 3)  // [4, 5, 3, 4, 5]
    
    // 将3号位复制到 0号位
    [1, 2, 3, 4, 5].copyWithin(0, 3, 4)  // [4, 2, 3, 4, 5]
    
    // -2相当于3号位,-1相当于4号位
    [1, 2, 3, 4, 5].copyWithin(0, -2, -1)  // [4, 2, 3, 4, 5]
    
    // 将3号位复制到 0号位
    [].copyWithin.call({length: 5, 3: 1}, 0, 3)// {0: 1, 3: 1, length: 5}
    
    
    • find()返回第一个符合条件的数组成员,如果没有符合条件的成员,则返回undefined
    //  第一个参数是回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组
    [1, 4, -5, 10].find((n) => n < 0)  // -5
    [1, 4, -5, 10].find((n) => n < -10)  // undefined
    [1, 5, 10, 15].find(function(value, index, arr) {
      return value > 9;
    }) // 10
    
    // 第二个参数,用来绑定回调函数的this对象
    function f(v){
      return v > this.age;
    }
    let person = {name: 'John', age: 20};
    [10, 12, 26, 15].find(f, person);    // 26
    
    • findIndex()返回第一个符合条件的数组成员的位置,如果没有符合条件的成员,则返回-1。
    [1, 4, -5, 10].findIndex((n) => n < 0)  // 2
    [1, 4, -5, 10].findIndex((n) => n < -10)  // -1
    [1, 5, 10, 15].findIndex(function(value, index, arr) {
      return value > 9;
    }) // 2
    
    [NaN].findIndex(y => Object.is(NaN, y))
    // 0
    
    • fill()使用给定值,填充一个数组
    ['a', 'b', 'c'].fill(7)
    // [7, 7, 7]
    
    new Array(3).fill(7)
    // [7, 7, 7]
    
    // 接受第二个和第三个参数,用于指定填充的起始位置和结束位置
    ['a', 'b', 'c'].fill(7, 1, 2)  // ['a', 7, 'c']
    
    // 填充的类型为对象,那么被赋值的是同一个内存地址的对象,而不是深拷贝对象
    let arr = new Array(3).fill({name: "Mike"});
    arr[0].name = "Ben";
    arr  // [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
    
    • keys()是对键名的遍历
    • values()是对键值的遍历
    • entries()是对键值对的遍历
    for (let index of ['a', 'b'].keys()) {
      console.log(index);
    }
    // 0
    // 1
    
    for (let elem of ['a', 'b'].values()) {
      console.log(elem);
    }
    // 'a'
    // 'b'
    
    for (let [index, elem] of ['a', 'b'].entries()) {
      console.log(index, elem);
    }
    // 0 "a"
    // 1 "b"
    
    • includes()返回一个布尔值,表示某个数组是否包含给定的值
    [1, 2, 3].includes(2)     // true
    [1, 2, 3].includes(4)     // false
    [1, 2, NaN].includes(NaN) // true
    //第二个参数表示搜索的起始位置
    [1, 2, 3].includes(3, 3);  // false
    [1, 2, 3].includes(3, -1); // true
    
    • flat() 【返回新数组】嵌套的数组“拉平”,变成一维的数组,默认是一层,Infinity不限制层数
    [1, 2, [3, 4]].flat()  // [1, 2, 3, 4]
    [1, 2, [3, 4]].flat(1)  // [1, 2, 3, 4]
    [1, 2, [3, [4, 5]]].flat()  // [1, 2, 3, [4, 5]]
    [1, 2, [3, [4, 5]]].flat(2)  // [1, 2, 3, 4, 5]
    [1, [2, [3]]].flat(Infinity)  // [1, 2, 3]
    [1, 2, , 4, 5].flat()  // [1, 2, 4, 5]
    
    • flatMap() 【返回新数组】对原数组的每个成员执行一次函数,然后对返回值组成的新数组执行flat()方法,只能展开一层数组
    // 相当于 [[2, 4], [3, 6], [4, 8]].flat()
    [2, 3, 4].flatMap((x) => [x, x * 2])
    // [2, 4, 3, 6, 4, 8]
    
    • sort()数组进行排序,常用插入排序、合并排序、冒泡排序等
    //按照字符编码的顺序进行排序,数组的元素转换成字符串,再进行比较
    [25, 1, 4, -5, 10].sort()  //[-5, 1, 10, 25, 4]
    
    //升序 : a大于b,返回大于零的数;a小于b,返回小于零的数;
    [25, 1, 4, -5, 10].sort((a,b)=>a-b)  // [-5, 1, 4, 10, 25]
    [25, 1, 4, -5, 10].sort((a,b)=>{
      return a - b > 0 ? 1 : -1;
    }) 
    // [-5, 1, 4, 10, 25]
    [{ name: "name1", age: 25 },{ name: "name2", age: 1 },{ name: "name3", age: 4 },{ name: "name4", age: -5 },{ name: "name5", age: 10 }].sort((a, b) => {
      return a.age - b.age > 0 ? 1 : -1;
    });
    //[{name: "name4", age: -5}, {name: "name2", age: 1}, {name: "name3", age: 4},{name: "name5", age: 10},{name: "name1", age: 25}]
    

    实例方法图解

    image.png

    扩展运算符...

    • 扩展运算符...将一个数组转为用逗号分隔的参数序列
    console.log(...[1, 2, 3])
    // 1 2 3
    console.log(1, ...[2, 3, 4], 5)
    // 1 2 3 4 5
    console.log([1, ...[2, 3, 4], 5])
    //[1, 2, 3, 4, 5]
    [...document.querySelectorAll('div')]
    // [<div>, <div>, <div>]
    
    //替代函数的 apply 方法
    // ES5 的写法
    Math.max.apply(null, [14, 3, 77])
    // ES6 的写法
    Math.max(...[14, 3, 77])
    // 等同于
    Math.max(14, 3, 77);
    
    // 复制数组
    const a1 = [1, 2];
    const a2 = [...a1];
    
    //  合并数组
    const arr1 = ['a', 'b'];
    const arr2 = ['c'];
    const arr3 = ['d', 'e'];
    // ES5 的合并数组
    arr1.concat(arr2, arr3);
    // [ 'a', 'b', 'c', 'd', 'e' ]
    // ES6 的合并数组
    [...arr1, ...arr2, ...arr3]
    // [ 'a', 'b', 'c', 'd', 'e' ]
    
    //解构赋值
    const [first, ...rest] = [1, 2, 3, 4, 5];
    first // 1
    rest  // [2, 3, 4, 5]
    
    // 字符串
    [...'hello']  // [ "h", "e", "l", "l", "o" ]
    

    数组的空位 empty

    • 空位是没有任何值,显示为empty,空位不是undefined,不是null,也不是空串""
    • forEach()filter()reduce()every()some()都会跳过空位
    • map()会跳过空位,但会保留这个值
    • join()toString()会将空位视为undefined,而undefinednull会被处理成空字符串
    • entries()keys()values()find()findIndex()会将空位处理成undefined
    [1,undefined,null,"",,2] //[1, undefined, null, "", empty, 2]
    0 in [undefined, undefined, undefined] // true
    0 in [, , ,] // false
    

    相关文章

      网友评论

          本文标题:数组 讲解之 扩展

          本文链接:https://www.haomeiwen.com/subject/nkrndktx.html