美文网首页
数组相关

数组相关

作者: 我家有个王胖胖 | 来源:发表于2022-06-22 00:03 被阅读0次

    1、数组搜索
    1.1判断数组中是否存在某个值
    ①indexOf


    indexOf.png

    ②lastIndexOf


    lastIndexOf.png

    ③includes


    includes.png

    ④find(返回符合条件的第一个元素,,无满足返回undefined)
    ⑤findIndex (返回符合条件的第一个元素的位置,无满足返回-1)

    ⑥filter(返回符合条件的所有元素的数组,无满足元素返回空数组)

    1.2 map方法


    map.png
    //若arr为[],map返回的也是[]
    let arr = [3000, 2000, 4500, 4200, 3500, 6000, 8000];
    //const result = arr.map(item => {
    //   return item * 2;
    //})
    const result = arr.map(item => item * 2)
    console.log(result);//[6000,  4000, 9000,  8400, 7000, 12000,16000]
    

    1.3reduce(累积操作)


    image.png
    //累加
    let arr = [2, 4, 6, 8]
    
    let result = arr.reduce((total, currentValue, currentIndex, arr) => {
        console.log(total);//10 12  16 22
        console.log(currentValue);//2,4,6,8
        console.log(currentIndex); //0,1,2,3
        console.log(arr); //[2, 4, 6, 8]
        return total + currentValue;
    }, 10)
    console.log(result);//30
    //求平均值
    let result = arr.reduce((total, currentValue, currentIndex, arr) => {
        total += currentValue;
        if (currentIndex == arr.length - 1) {
            return total / arr.length;
        }
        return total;
    });
    
    console.log(result);
    //数组扁平化
    //reduce
    let arr = [
        1, [1, 2],
        [3, 4],
        [5, 6]
    ]
    let result = arr.reduce((total, currentValue) => total.concat(currentValue), []);
    
    console.log(result);
    //递归
    function flatten(arr) {
        let result = [];
        for (let i = 0; i < arr.length; i++) {
            if (Array.isArray(arr[i])) {
                result = result.concat(flatten(arr[i]));
            } else {
                result.push(arr[i]);
            }
        }
        return result;
    }
    console.log(flatten(arr));
    //拓展运算符
    unction flatten(arr) {
        while (arr.some(item => {
                Array.isArray(item)
            })) {
            return [].concat(...arr)
        }
    }
    console.log(flatten(arr));
    console.log([].concat(...arr));
    //toString + split
    let result2 = arr.toString();
    console.log(result2.split(','));
    //flat
    console.log(arr.flat());
    //
    

    1.4数组排序

    //升序
    let arr = ["Orange", "Apple", "Grapes", "Banana"];
    let arr2 = [5, 40, 3, 9, 6];
    console.log(arr.sort());//[ 'Apple', 'Banana', 'Grapes', 'Orange' ]
    console.log(arr2.sort());//[ 3, 40, 5, 6, 9 ]
    console.log(arr2.sort((a, b) => {//[ 3, 5, 6, 9, 40 
        return a - b;
    }));
    //字母降序
    //先升序再 reverse
    let arr = ["Orange", "Apple", "Grapes", "Banana"];
    console.log(arr2.sort().reverse());
    //如果数组中有undefined,undefined排在最后
    //对象排序
    //对象排序
    const employess = [
        { eNo: 1001, salary: 3000 },
        { eNo: 1002, salary: 2200 },
        { eNo: 1003, salary: 3400 },
        { eNo: 1004, salary: 5000 },
    ]
    
    employess.sort((a, b) => {
        return a.salary - b.salary
    })
    console.log(employess);
    //[
      //{ eNo: 1002, salary: 2200 },
      //{ eNo: 1001, salary: 3000 },
      //{ eNo: 1003, salary: 3400 },
      //{ eNo: 1004, salary: 5000 } 
    //]
    

    1.5数组解构

    let arr = [1,2,3,4];
    let [a,b,c,d] = arr;
    console.log(a,b,c,d);//1 2 3 4
    //不完全结构
    let arr = [1,2,3,4];
    let [a,,c,d] = arr;
    console.log(a,,c,d); // 1 3 4
    //值交换
    let a = 4;
    let b = 5;
    let [a,b] = let [b,a]
    //与拓展运算符一起使用
    let arr = [4, 5, 6];
    let [a, ...b] = arr;
    console.log(a);
    console.log(b);
    

    相关文章

      网友评论

          本文标题:数组相关

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