• js数组常用方法大全


    数组是js中最重要的数据类型之一,掌握数组方法对前端学习和工作都有莫大帮助。数组方法繁多,不便记忆,这里给数组方法分门别类,只对方法功能进行简单的介绍,帮助大家对数组有一个系统全面的了解,日后遇到时再进行重点学习,如有遗漏错误,欢迎在评论区指正。

     给大家推荐一个实用面试题库

    1、前端面试题库 (面试必备)            推荐:★★★★★

    地址:web前端面试题库

    一、增删改方法

    增删改查四大天王是数组中最常见也是最简单的方法,需要留意的是哪些方法会对原数组产生影响,哪些方法不会,查找方法较多,单独说明

    下面前五种增删方法都对原数组产生影响

    • push()
    • unshift()
    • pop()
    • shift()
    • splice()

    push()

    push()方法接收任意数量的参数,并将它们添加到数组末尾,返回数组的最新长度

    1. let colors = ["red"]; // 创建一个数组
    2. let count = colors.push("green","blue"); // 推入两项
    3. console.log(count) // 3
    4. console.log(colors) // ["red","green","blue"]

    unshift()

    unshift()在数组开头添加任意多个值,然后返回新的数组长度

    1. let colors = ["red"]; // 创建一个数组
    2. let count = colors.unshift("green","blue"); // 从数组开头推入两项
    3. console.log(count) // 3
    4. console.log(colors) // ["green","blue","red"]

    pop()

    pop() 方法用于删除数组的最后一项,同时减少数组的 length 值,返回被删除的项

    1. let colors = ["red","green","blue"]
    2. let item = colors.pop(); // 取得最后一项
    3. console.log(item) // blue
    4. console.log(colors) // ["red","green"]

    shift()

    shift()方法用于删除数组的第一项,同时减少数组的 length 值,返回被删除的项

    1. let colors = ["red","green","blue"]
    2. let item = colors.shift(); // 取得第一项
    3. console.log(item) // red
    4. console.log(colors) // ["green","blue"]

    splice()

    splice功能强大,可以同时对数组进行增删改的操作

    传入三个参数,分别是开始位置,要删除元素的数量,要插入的任意多个元素,返回删除元素的数组,对原数组产生影响

    1. let colors = ["red", "green", "blue"];
    2. let removed = colors.splice(1, 1, "red", "purple"); // 插入两个值,删除一个元素
    3. console.log(colors); // red,red,purple,blue
    4. console.log(removed); // green,只有一个元素的数组

    删除:传入两个参数,分别是开始下标位置,删除元素的数量,返回包含删除元素的数组

    1. let colors = ["red", "green", "blue"];
    2. let removed = colors.splice(0,1); // 删除第一项
    3. console.log(colors); // green,blue
    4. console.log(removed); // red,只有一个元素的数组

    增加:传入三个参数,分别是开始位置、0(要删除的元素数量)、插入的元素,返回空数组

    1. let colors = ["red", "green", "blue"];
    2. let removed = colors.splice(1, 0, "yellow", "orange")
    3. console.log(colors) // red,yellow,orange,green,blue
    4. console.log(removed) // []

    :只需要在删除的位置添加元素即可

    1. let colors = ["red", "green", "blue"];
    2. let removed = colors.splice(1, 1, "yellow")
    3. console.log(colors) // red,yellow,blue
    4. console.log(removed) // ["green"]

    二、搜索和位置方法

    即查找元素,返回元素坐标或者元素值

    • indexOf()
    • lastIndexOf()
    • find()
    • findIndex()
    • includes()

    indexOf()

    返回要查找的元素在数组中第一次出现的下标,如果没找到则返回 -1

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. console.log(numbers.indexOf(9) ); // -1
    3. console.log(numbers.indexOf(4) ); // 3

    lastIndexOf()

    返回要查找的元素在数组中最后一次出现的下标,如果没找到则返回 -1

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. console.log(numbers.lastIndexOf(9) ); // -1
    3. console.log(numbers.lastIndexOf(4) ); // 5

    find()

    返回第一个匹配的元素

    1. const people = [
    2. {
    3. name: "张三",
    4. age: 27
    5. },
    6. {
    7. name: "李四",
    8. age: 29
    9. }
    10. ];
    11. people.find((item, index, array) => item.age < 28) // // {name: "Matt", age: 27}

    findIndex()

    返回满足条件的数组下标,找不到则返回 -1

    1. const people = [
    2. {
    3. name: "张三",
    4. age: 27
    5. },
    6. {
    7. name: "李四",
    8. age: 29
    9. }
    10. ];
    11. people.findIndex((item, index, array) => item.age === 28) // -1
    12. people.findIndex((item, index, array) => item.age < 28) // 0

    includes()

    返回要查找的元素在数组中的位置,找到返回true,否则false

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. numbers.includes(4) // true
    3. numbers.includes(9) // false

    三、排序方法

    数组有两个方法可以用来对元素重新排序:

    • reverse()
    • sort()

    reverse()

    将数组元素方向反转,改变原数组

    1. let values = [1, 2, 3, 4, 5];
    2. values.reverse();
    3. console.log(values) // [5,4,3,2,1]

    sort()

    sort()方法接受一个回调函数,回调中两个参数的顺序决定升序或是降序

    1. //升序
    2. let values = [4, 2, 3, 1, 5];
    3. values.sort((a,b) => a - b)
    4. console.log(values) //[1, 2, 3, 4, 5]
    5. //降序
    6. values.sort((a,b) => b - a)
    7. console.log(values) //[5, 4, 3, 2, 1]

    四、操作方法方法

    常见的操作方法有三种:

    join()

    join() 将数组转换为字符串,接收一个参数,作为数组每一项间的连接符,影响原数组

    1. let colors = ["red", "green", "blue"];
    2. console.log(colors.join(",")) // red,green,blue
    3. console.log(colors.join("||")) // red||green||blue

    slice()

    slice() 截取数组中一部分,用于创建一个包含原有数组中一个或多个元素的新数组,接受两个参数,第一个是开始截取的下标,第二个是结束的下标,不包含这个值,如果只有一个参数,则截取从下标开始的所有项,不会影响原始数组

    1. let colors = ["red", "green", "blue", "yellow", "purple"];
    2. let colors2 = colors.slice(1);
    3. let colors3 = colors.slice(1, 4);
    4. console.log(colors) // red,green,blue,yellow,purple
    5. concole.log(colors2); // green,blue,yellow,purple
    6. concole.log(colors3); // green,blue,yellow

    concat()

    concat()会连接两个数组,首先会创建一个当前数组的副本,然后再把它的参数添加到副本末尾,最后返回这个新构建的数组,不会影响原始数组

    1. let colors = ["red", "green", "blue"];
    2. let colors2 = colors.concat("yellow", ["black", "brown"]);
    3. console.log(colors); // ["red", "green","blue"]
    4. console.log(colors2); // ["red", "green", "blue", "yellow", "black", "brown"]

    五、迭代方法

    常用来迭代数组的方法(都不改变原数组)有如下:

    • some()
    • every()
    • forEach()
    • filter()
    • map()
    • reduce()

    some()

    对数组中的每一项都执行回调函数,如果有一项满足函数的条件,则这个方法返回 true

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. let someResult = numbers.every((item, index, array) => item > 2);
    3. console.log(someResult) // true

    every()

    对数组中的每一项都执行回调函数,如果每一项都满足函数的条件 ,则这个方法返回 true

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. let everyResult = numbers.every((item, index, array) => item > 2);
    3. console.log(everyResult) // false

    forEach()

    对数组中的每一项都执行回调函数,没有返回值,与for循环相似

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. numbers.forEach((item, index, array) => {
    3. // 执行某些操作
    4. });

    filter()

    对数组中的每一项都执行回调函数,返回满足条件的项组成新数组之后返回

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. let neWnumbers = numbers.filter((item, index, array) => item > 2);
    3. console.log(neWnumbers); // [3,4,5,4,3]

    map()

    对数组中的每一项都执行回调函数,返回由每次函数调用的结果构成的数组

    1. let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
    2. let mapResult = numbers.map((item, index, array) => item * 2);
    3. console.log(mapResult) // [2,4,6,8,10,8,6,4,2]

    reduce()

    对数组进行累加操作,接受两个参数,第一个参数为回调函数,如果没有第二个参数,pre的值为数组的第一项,pre与第二项item相加后的值重新作为pre值与第三项相加,直到最后一项,如果有第二个参数,则pre的初始值为第二个参数与数组第一项相加

    1. const arr = [1,2,3,4,5]
    2. arr.reduce((pre,item,arr) => {
    3. pre += item
    4. return pre
    5. },0)

     给大家推荐一个实用面试题库

    1、前端面试题库 (面试必备)            推荐:★★★★★

    地址:web前端面试题库

     

  • 相关阅读:
    【目标跟踪】多目标跟踪测距
    《linux程序设计》笔记第一章
    逍遥自在学C语言 | 算数运算符
    Windows平台下将exe及其dll封包到新的exe
    CentOS7系统下安装OpenOffice
    golang中的字符串
    【Linux】简化自用-Win10安装VMware和CentOS
    LabVIEW专栏九、类的应用
    【go零基础】go-zero从零基础学习到实战教程 - 0环境配置
    计算机网络基本概念
  • 原文地址:https://blog.csdn.net/weixin_42981560/article/details/133995655