js数组常用方法大全 | 您所在的位置:网站首页 › js数组操作方法大全图片 › js数组常用方法大全 |
数组是js中最重要的数据类型之一,掌握数组方法对前端学习和工作都有莫大帮助。数组方法繁多,不便记忆,这里给数组方法分门别类,只对方法功能进行简单的介绍,帮助大家对数组有一个系统全面的了解,日后遇到时再进行重点学习,如有遗漏错误,欢迎在评论区指正。 增删改查四大天王是数组中最常见也是最简单的方法,需要留意的是哪些方法会对原数组产生影响,哪些方法不会,查找方法较多,单独说明 下面前五种增删方法都对原数组产生影响 push() unshift() pop() shift() splice() push()push()方法接收任意数量的参数,并将它们添加到数组末尾,返回数组的最新长度 let colors = ["red"]; let count = colors.push("green","blue"); console.log(count) console.log(colors) unshift()unshift()在数组开头添加任意多个值,然后返回新的数组长度 let colors = ["red"]; let count = colors.unshift("green","blue"); console.log(count) console.log(colors) pop()pop() 方法用于删除数组的最后一项,同时减少数组的 length 值,返回被删除的项 let colors = ["red","green","blue"] let item = colors.pop(); console.log(item) console.log(colors) shift()shift()方法用于删除数组的第一项,同时减少数组的 length 值,返回被删除的项 let colors = ["red","green","blue"] let item = colors.shift(); console.log(item) console.log(colors) splice()splice功能强大,可以同时对数组进行增删改的操作 传入三个参数,分别是开始位置,要删除元素的数量,要插入的任意多个元素,返回删除元素的数组,对原数组产生影响 let colors = ["red", "green", "blue"] let removed = colors.splice(1, 1, "red", "purple") console.log(colors) console.log(removed)删除:传入两个参数,分别是开始下标位置,删除元素的数量,返回包含删除元素的数组 let colors = ["red", "green", "blue"] let removed = colors.splice(0,1) console.log(colors) console.log(removed)增加:传入三个参数,分别是开始位置、0(要删除的元素数量)、插入的元素,返回空数组 let colors = ["red", "green", "blue"] let removed = colors.splice(1, 0, "yellow", "orange") console.log(colors) // red,yellow,orange,green,blue console.log(removed) // []改:只需要在删除的位置添加元素即可 let colors = ["red", "green", "blue"] let removed = colors.splice(1, 1, "yellow") console.log(colors) // red,yellow,blue console.log(removed) // ["green"]即查找元素,返回元素坐标或者元素值 indexOf() lastIndexOf() find() findIndex() includes() indexOf()返回要查找的元素在数组中第一次出现的下标,如果没找到则返回 -1 let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] console.log(numbers.indexOf(9) ) console.log(numbers.indexOf(4) ) lastIndexOf()返回要查找的元素在数组中最后一次出现的下标,如果没找到则返回 -1 let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] console.log(numbers.lastIndexOf(9) ) console.log(numbers.lastIndexOf(4) ) find()返回第一个匹配的元素 const people = [ { name: "张三", age: 27 }, { name: "李四", age: 29 } ]; people.find((item, index, array) => item.age item.age === 28) people.findIndex((item, index, array) => item.age a - b) console.log(values) //[1, 2, 3, 4, 5] //降序 values.sort((a,b) => b - a) console.log(values) //[5, 4, 3, 2, 1]常见的操作方法有三种: join()join() 将数组转换为字符串,接收一个参数,作为数组每一项间的连接符,影响原数组 let colors = ["red", "green", "blue"]; console.log(colors.join(",")) console.log(colors.join("||")) slice()slice() 截取数组中一部分,用于创建一个包含原有数组中一个或多个元素的新数组,接受两个参数,第一个是开始截取的下标,第二个是结束的下标,不包含这个值,如果只有一个参数,则截取从下标开始的所有项,不会影响原始数组 let colors = ["red", "green", "blue", "yellow", "purple"] let colors2 = colors.slice(1) let colors3 = colors.slice(1, 4) console.log(colors) // red,green,blue,yellow,purple concole.log(colors2) concole.log(colors3) concat()concat()会连接两个数组,首先会创建一个当前数组的副本,然后再把它的参数添加到副本末尾,最后返回这个新构建的数组,不会影响原始数组 let colors = ["red", "green", "blue"]; let colors2 = colors.concat("yellow", ["black", "brown"]); console.log(colors); // ["red", "green","blue"] console.log(colors2); // ["red", "green", "blue", "yellow", "black", "brown"]常用来迭代数组的方法(都不改变原数组)有如下: some() every() forEach() filter() map() reduce() some()对数组中的每一项都执行回调函数,如果有一项满足函数的条件,则这个方法返回 true let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] let someResult = numbers.every((item, index, array) => item > 2) console.log(someResult) // true every()对数组中的每一项都执行回调函数,如果每一项都满足函数的条件 ,则这个方法返回 true let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] let everyResult = numbers.every((item, index, array) => item > 2) console.log(everyResult) // false forEach()对数组中的每一项都执行回调函数,没有返回值,与for循环相似 let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] numbers.forEach((item, index, array) => { // 执行某些操作 }) filter()对数组中的每一项都执行回调函数,返回满足条件的项组成新数组之后返回 let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] let neWnumbers = numbers.filter((item, index, array) => item > 2) console.log(neWnumbers) map()对数组中的每一项都执行回调函数,返回由每次函数调用的结果构成的数组 let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1] let mapResult = numbers.map((item, index, array) => item * 2) console.log(mapResult) // [2,4,6,8,10,8,6,4,2] reduce()对数组进行累加操作,接受两个参数,第一个参数为回调函数,如果没有第二个参数,pre的值为数组的第一项,pre与第二项item相加后的值重新作为pre值与第三项相加,直到最后一项,如果有第二个参数,则pre的初始值为第二个参数与数组第一项相加 const arr = [1,2,3,4,5] arr.reduce((pre,item,arr) => { pre += item return pre },0) |
CopyRight 2018-2019 实验室设备网 版权所有 |