javaScript常用数组方法

javaScript常用数组方法

在 Javascript 中,数组的使用十分频繁,故数组方法是编程小白必备的技能之一。

方法方法名功能返回值是否改变原数组版本支持
1push()向数组中添加一个或者多个元素(数组尾)返回新数组的长度ES5+
2unshift()向数组中添加一个或者多个元素(数组头)返回新数组的长度ES5+
3pop()移除数组最后一位返回被删除的数据ES5+
4shift()移除数组的第一位返回被删除的数据ES5+
5reverse()反转数组中的元素返回新数组ES5+
6sort()以字母顺序对数组进行排序返回新数组ES5+
7splice()在指定位置删除指定个数元素,再增加任意元素个数返回删除的数据ES5+
8concat()通过合并现有数组创建新数组返回新数组ES5+
9join()用特定字符讲数组拼接成字符串(默认为",")返回拼接后的字符串ES5+
10slice()裁剪指定位置的数组被裁减元素所构成的新数组ES5+
11toString()将数组转换为字符串新数组ES5+
12valueOf()查询数组的原始值数组的原始值ES5+
13indexOf()查询某个数组在数组中第一次出现的位置(返回索引)存在该元素返回其下标,不存在返回-1ES5+
14lastIdexOf()反向查询某个数组在数组中第一次出现的位置(返回索引)存在该元素返回其下标,不存在返回-1ES5+
15forEach()(迭代)遍历数组,每次循环执行传入的回调函数无(undefined)ES5+
16map()(迭代) 遍历数组, 每次循环时执行传入的回调函数,根据回调函数的返回值,生成一个新的数组有(自定义)ES5+
17filter()(迭代) 遍历数组, 每次循环时执行传入的回调函数,回调函数返回一个条件,把满足条件的元素筛选出来放到新数组中满足条件的元素组成的新数组ES5+
18every()(迭代) 判断数组中所有的元素是否满足某个条件全都满足返回true 只要有一个不满足 返回falseES5+
19some()(迭代) 判断数组中是否存在,满足某个条件的元素只要有一个元素满足条件就返回true,都不满足返回falseES5+
20reduce()(归并)遍历数组, 每次循环时执行传入的回调函数,回调函数会返回一个值,将该值作为初始值prev,传入到下一次函数中最终操作的结果ES5+
21reduceRight()(归并)用法同reduce,只不过是从右向左最终操作的结果ES5+
22includes()判断一个数组是否包含一个指定的值是返回 true,否则falseES6+
23Array.from()接收伪数组,返回对应的真数组对应的真数组ES6+
24find()遍历数组,执行回调函数,回调函数执行一个条件,返回满足条件的第一个元素,不存在返回undefined满足条件第一个元素/否则返回undefinedES6+
25findIndex()遍历数组,执行回调函数,回调函数接受一个条件,返回满足条件的第一个元素下标,不存在返回-1满足条件第一个元素下标,不存在=>-1ES6+
26fill()用给定值填充一个数组新数组ES6+
27flat()用于将嵌套的数组“拉平”,变成一维的数组返回一个新数组ES6+
28flatMap()flat()和map()的组合版 , 先通过map()返回一个新数组,再将数组拉平( 只能拉平一次 )返回新数组ES6+

1. push()

  • push() 方法向数组尾部添加一个或者多个元素,元素间用逗号分隔
  • 返回值为添加元素后的数组长度
var arr = [1, 2, 3];
arr.push(4);

console.log(arr);
// expected output: [ 1, 2, 3, 4 ]

arr.push('a', 'b', 'c');
console.log(arr);
// expected output: [1, 2, 3, 4, 'a', 'b', 'c']

var lens = arr.push('hell JS')
console.log(lens);
// expected output: 8

2. unshift()

  • unshift() 方法向数组头部添加一个或者多个元素,元素间用逗号分隔
  • 返回值为添加元素后的数组长度
// unshift 方法
var arr = [1, 2, 3];
arr.unshift(4);

console.log(arr);
// expected output: [ 4, 1, 2, 3]

arr.unshift('a', 'b', 'c');
console.log(arr);
// expected output: ['a', 'b', 'c', 4, 1, 2, 3]

var lens = arr.unshift('hell JS')
console.log(lens);
// expected output: 8

3. pop()

  • 删除数组最后一位,该方法不接受参数
  • 返回值为被删除元素的值
var arr = [1, 2, 3];

arr.pop();
console.log(arr);
// expected output: [1, 2]

var item = arr.pop();
console.log(item);
// expected output: 2

4. shift()

  • 删除数组第一位元素,该方法不接受参数
  • 返回值为被删除元素的值
var arr = [1, 2, 3];

arr.shift();
console.log(arr);
// expected output: [2, 3]

var item = arr.shift();
console.log(item);
// expected output: 2

5.reverse()

  • 反转数组元素
  • 该方法会改变原数组
var arr = [1, 2, 3, 'a', 'b', 'c']

arr.reverse();
console.log(arr);
// expected output:  ['c', 'b', 'a', 3, 2, 1]

6.sort()

  • 对数组元素进行排序,但排序依据 unicode 编码
  • 返回值为一个数组
  • 影响原数组
var arr = [9, 5, 2, 7, 21, 51];

arr.sort();
console.log(arr);
// expected output: [2, 21, 5, 51, 7, 9]

上面代码块中排序结果和我们正常期望结果不同,可以通过添加参数来解决。

语法:sort(function(a,b))

var arr = [9, 5, 2, 7, 21, 51];

// ————————————升序排序————————————
arr.sort(function(a, b) {
  	return a - b;
})
console.log(arr);
// expected output: [2, 5, 7, 9, 21, 51]

// ————————————降序排序————————————
arr.sort(function(a, b) {
    return b - a;
})
console.log(arr);
// expected output: [51, 21, 9, 7, 5, 2]

元素为对象时。可以根据属性值来对其进行排序

        // 元素为对象时
        var arr = [{
            name: '张三',
            age: 18
        }, {
            name: '李四',
            age: 28
        }, {
            name: '王五',
            age: 38
        }]

        arr.sort(function(a, b) {
            return b.age - a.age;
        // 降序排序
        })

        console.log(arr);
        // expected output: 
        //         [
        //     {
        //         "name": "王五",
        //         "age": 38
        //     },
        //     {
        //         "name": "李四",
        //         "age": 28
        //     },
        //     {
        //         "name": "张三",
        //         "age": 18
        //     }
        // ]

7.splice()

  • 一共接受三个参数,splice( index, howmany,[ item1…,item5])
  • index 索引(必须),从该索引开始删除元素
  • howmany(必须),删除几个元素
  • [ item1,…,itemn],新添加的元素
        // splice() 方法
        var arr1 = [0, 1, 2, 3, 4, 5, 6]

        arr1.splice(1, 2);
        console.log(arr1);
        // expected output: [0, 3, 4, 5, 6]

        var arr2 = [0, 1, 2, 3, 4, 5, 6]
        arr2.splice(1, 2, 'a', 'b')
        console.log(arr2);
        // expected output: [0, 'a', 'b', 3, 4, 5, 6]

8. contact()

  • 将已有数组拼接为新数组
  • 返回值为一个新数组
  • 不改变原数组
  • 若没有参数,该方法会复制一个数组
// contact() 方法
var arr1 = [1, 2, 3];
var arr2 = ['a', 'b', 'c'];
var arr3 = ['张三', '李四', '王五'];

var newArr = arr1.concat(arr2, arr3);
console.log(newArr);
// expected output: [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五']

9. join()

  • 将数组的所有元素连接成一个字符串
// join() 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];

var str1 = arr.join()
console.log(typeof str1);
// expected output: string

console.log(str1);
// expected output: 1,2,3,a,b,c,张三,李四,王五

var str2 = arr.join('');
console.log(str2);
// expected output: 123abc张三李四王五

var str3 = arr.join('-');
console.log(str3);
// expected output: 1-2-3-a-b-c-张三-李四-王五

10.slice()

  • 截取数组的一部分,并返回新数组
  • slice(startIndex, endIndex),截取开始索引和结束索引之间的字符串
  • 返回一个新的字符串
  • 若没有参数,该方法会复制一个数组
// slice() 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];
var newArr = arr.slice(2, 5);
console.log(newArr);
// expected output: [3, 'a', 'b']

11. toString()

  • 将数组转换为字符串,并且返回转换后的新数组
  • 不改变原数组
  • 与 join() 方法不添加任何参数相同
// toString 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];

var newArr = arr.toString();
console.log(newArr);
// expected output: 1,2,3,a,b,c,张三,李四,王五

12. valueOf()

  • 返回数组的原始值
  • valueOf() 方法返回数组。
  • valueOf() 方法是数组对象的默认方法。
// valueOf 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];

var newArr = arr.valueOf()
console.log(newArr);
// expected output: [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五']

13. indexOf()

  • indexOf() 方法在数组中搜索指定项目,并返回其位置。
  • 搜索将从指定位置开始,如果未指定开始位置,则从头开始,并在数组末尾结束搜索。
  • 如果未找到该项目,则 indexOf() 返回 -1。
  • 如果该项目出现多次,则 indexOf() 方法返回第一次出现的位置。
// indexOf() 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];

var index = arr.indexOf('张三');
console.log(index);
// expected output: 6

var index2 = arr.indexOf('张三三');
console.log(index2);
// expected output: -1

14.lastIdexOf()

  • lastIndexOf() 方法在数组中搜索指定项目,并返回其位置
  • 搜索将从指定位置开始,如果未指定开始位置,则从末尾开始,并在数组的开头结束搜索
  • 如果未找到该项目,则 lastIndexOf() 方法返回 -1
  • 如果要搜索的项目不止一次出现,lastIndexOf() 方法将返回最后一次出现的位置
// lastIndexOf() 方法
var arr = [1, 2, 3, 'a', 'b', 'c', '张三', '李四', '王五'];

var index = arr.lastIndexOf('张三');
console.log(index);
// expected output: 6

var index2 = arr.lastIndexOf('张三三');
console.log(index2);
// expected output: -1

15.forEach()

  • forEach() 方法按顺序为数组中的每个元素调用一次函数
  • **注:**对于没有值的数组元素,不执行forEach() 方法
  • 语法:array.forEach(function(currentValue, index, arr), thisValue)
// forEach() 方法
var arr = ['a', 'b', 'c']
arr.forEach(function(item, index, arr) {
        console.log('元素的值:' + item + '元素的索引:' + index + '元素的所属的数组:' + arr);
    })
    // expected output:
    // 元素的值:a 元素的索引:0 元素的所属的数组:a,b,c
    // 元素的值:b 元素的索引:1 元素的所属的数组:a,b,c
    // 元素的值:c 元素的索引:2 元素的所属的数组:a,b,c

16. map()

  • map() 方法使用为每个数组元素调用函数的结果创建新数组。
  • map() 方法按顺序为数组中的每个元素调用一次提供的函数。
  • 注释:map() 对没有值的数组元素不执行函数。
  • 注释:map() 不会改变原始数组。
  • map() ,方法可以有返回值
// map() 方法
var arr = [1, 2, 3, 4, 5]
var newArr = arr.map(function(item, index, arr) {
    return item * item;
})
console.log(newArr);
// expected output: [1, 4, 9, 16, 25]

17. filter()

  • 使用数组中通过测试的每个元素创建新数组。
// filter() 方法
var arr = [1, 2, 3, 4, 5];

var newArr = arr.filter(function(item, idex, arr) {
    return item > 2;
})
console.log(newArr);
// expected output: [3, 4, 5]

18. every()

  • 检查数组中的每个元素是否通过测试。

  • every() 方法检查数组中的所有元素是否都通过了测试(被作为函数提供)。

  • every() 方法对数组中存在的每个元素执行一次函数:

    • 如果找到函数返回 false 值的数组元素,every() 返回 false(并且不检查剩余值)
    • 如果没有出现 false,every() 返回 true

    注释:every() 不对没有值的数组元素执行函数。

    注释:every() 不改变原始数组。

// every() 方法
var arr = [1, 2, 3, 4, 5];

var result = arr.every(function(item, idex, arr) {
    return item > 2;
})
console.log(result);
// expected output: flase

19.some()

  • 检查数组中的任何元素是否通过测试。

  • some() 方法检查数组中的任何元素是否通过测试(作为函数提供)。

  • some() 方法对数组中存在的每个元素执行一次函数:

    • 如果找到函数返回真值的数组元素,some() 返回真(并且不检查剩余值)
    • 否则返回 false

    注释:some() 对没有值的数组元素不执行函数。

    注释:some() 不改变原始数组。

// some() 方法
var arr = [1, 2, 3, 4, 5];

var result = arr.some(function(item, idex, arr) {
    return item > 2;
})
console.log(result);
// expected output: true

20.reduce()

  • 语法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

  • reduce() 方法将数组缩减为单个值。

  • reduce() 方法为数组的每个值(从左到右)执行提供的函数。

  • 函数的返回值存储在累加器中(结果/总计)。

    **注释:**对没有值的数组元素,不执行 reduce() 方法。

    注释:reduce() 方法不会改变原始数组。

当 reduce 方法中的 function()的行参 pre 没有初始值时

// reduce() 方法,当 reduce 方法中的 function()的行参 pre 没有初始值时
var arr = [0, 1, 2, 3, 4, 5];
var result = arr.reduce(function(pre, item, index, arr) {
    console.log('执行此函数前的值:' + pre + '当前元素的值:' + item + '当前元素索引' + '当前元素所在数组' + arr);
    return pre + item;
})
console.log(result);
// expected output:
// reduce 方法中的 function(),一共执行五次,将数组索引为 0 的作为行参 pre 的初始值
// 执行此函数前的值:0当前元素的值:1当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:1当前元素的值:2当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:3当前元素的值:3当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:6当前元素的值:4当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:10当前元素的值:5当前元素索引当前元素所在数组0,1,2,3,4,5
// 输出结果 reslut = 15

当 reduce 方法中的 function()的行参 pre 有初始值时

// reduce() 方法,当 reduce 方法中的 function()的行参 pre 有初始值: => 0;
var arr = [0, 1, 2, 3, 4, 5];
var result = arr.reduce(function(pre, item, index, arr) {
    console.log('执行此函数前的值:' + pre + '当前元素的值:' + item + '当前元素索引' + '当前元素所在数组' + arr);
    return pre + item;
}, 0)
console.log(result);
// expected output:
// reduce 方法中的 function(),一共执行六次,将参数 0 的作为行参 pre 的初始值
// 执行此函数前的值:0当前元素的值:0当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:0当前元素的值:1当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:1当前元素的值:2当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:3当前元素的值:3当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:6当前元素的值:4当前元素索引当前元素所在数组0,1,2,3,4,5
// 执行此函数前的值:10当前元素的值:5当前元素索引当前元素所在数组0,1,2,3,4,5
// 输出结果 reslut = 15

21.reduceRight()

  • 同 reduce() 但执行方向为数组尾部向数组头部

22.includes()

  • 检查数组是否包含指定的元素

  • 如果数组包含元素,则此方法返回 true,否则返回 false

    注释:includes() 方法区分大小写。

// includes() 方法
let arr = ['张飞', '赵云', '黄忠', '关羽', '马超']

let reslut1 = arr.includes('关羽')
console.log(reslut1);
// expected output: true

let reslut2 = arr.includes('刘备')
console.log(reslut2);
// expected output: flase

23.Array.from()

  • from() 方法从具有 length 属性或可迭代对象的任何对象返回 Array 对象。
  • 将一个类数组对象转换为一个真正的数组,必须具备以下条件:
    • 该 伪数组 / 类数组 对象必须具有length属性,用于指定数组的长度。如果没有length属性,那么转换后的数组是一个空数组。
    • 该 伪数组 / 类数组 对象的属性名必须为数值型或字符串型的数字
// from() 方法
let object1 = {
    0: '张三',
    1: '18',
    2: '男',
    length: 3
}
let arr = Array.from(object1)
console.log(arr);
// expected output: ['张三', '18', '男']

24.find()

  • 返回数组中第一个通过测试的元素的值。

  • find() 方法返回数组中第一个通过测试的元素的值(作为函数提供)。

  • find() 方法对数组中存在的每个元素执行一次函数:

    • 如果找到函数返回 true 值的数组元素,则 find() 返回该数组元素的值(并且不检查剩余值)
    • 否则返回 undefined

    注释:find() 不对空数组执行该函数。

    注释:find() 不会改变原始数组。

// find() 方法
let arr = [1, 2, 3, 4];

let reslut = arr.find(function(item, index, arr) {
    console.log('当前元素的值:' + item + '当前元素索引' + '当前元素所在数组' + arr);
    return item > 3;
})

console.log(reslut);
// expected output: 
// 当前元素的值:1当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:2当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:3当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:4当前元素索引当前元素所在数组1,2,3,4
// 输出结果:reslut = 4;

25.findIndex()

  • 返回数组中通过测试的第一个元素的索引

  • findIndex() 方法返回数组中通过测试的第一个元素的索引(作为函数提供)。

  • findIndex() 方法对数组中存在的每个元素执行一次函数:

    • 如果找到函数返回 true 值的数组元素,则 findIndex() 返回该数组元素的索引(并且不检查剩余值)
    • 否则返回 -1

    注释:findIndex() 不会为没有值的数组元素执行函数。

    注释:findIndex() 不会改变原始数组。

let arr = [1, 2, 3, 4];

let reslutIndex = arr.findIndex(function(item, index, arr) {
    console.log('当前元素的值:' + item + '当前元素索引' + '当前元素所在数组' + arr);
    return item > 3;
})

console.log(reslutIndex);
// expected output: 
// 当前元素的值:1当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:2当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:3当前元素索引当前元素所在数组1,2,3,4
// 当前元素的值:4当前元素索引当前元素所在数组1,2,3,4
// 输出结果:reslutIndex = 3;

26.fill()

  • array.fill(value, start, end)
  • 用静态值填充数组中的元素。
  • fill() 方法用静态值填充数组中的指定元素。
  • 可以指定开始和结束填充的位置。如果未指定,则将填充所有元素。
  • 原数组改变
// fill() 方法
let arr = [1, 2, 3, 4];

arr.fill('填充值', 1, 3);
console.log(arr);
// expected output: [1, '填充值', '填充值', 4]

27.flat()

  • 扁平化数组,高维数组转化地位数组
  • 返回一个新数组
//flat() 方法
let arr = [1, 2, [3, [4, 5]]];

let arr1 = arr.flat();
console.log(arr1);
// expected output: [1, 2, 3, [4, 5]]

let arr2 = [1, 2, [3, [4, 5]]]

let newArr = arr2.flat(2);
console.log(newArr);
// expected output: [1, 2, 3, 4, 5]

28.flatMap()

  • flat()和map()的组合版,先通过map()返回一个新数组,再进行数组扁平化(一次)
// flatMap() 方法
let arr = [1, 2, 3, 4, 5];
let newArr1 = arr.map(function(item, index) {
    return [item * item]
})
console.log(newArr1);
// expected output: [[1], [4], [9], [16], [25]]

let newArr2 = arr.flatMap(function(item, index) {
    return [item * item]
})
console.log(newArr2);
// expected output: [1, 4, 9, 16, 25]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值