数组方法总结

测试用例

let fruits = ['apple', 'orange', 'Banana']
let animals = ['dog','cat']
let numbers = ['one','two']
let num = [1,2,3]
let num1 = [2, 3, 4, 5]

1. array.foreach()

  • 方法含义:遍历数组
  • 代码演示:
let text = fruits.forEach(item =>{
 console.log(item); 
})

2. array.push()

  • 方法含义:添加元素到数组的末尾
  • 返回值:添加后的数据长度
  • 注意:修改原数组
  • 代码演示:
let text = fruits.push('Lemon')
console.log(text); // 5
console.log(fruits); //  ['apple', 'orange', 'Banana', 'Lemon']

3. array.pop()

  • 方法含义:删除数组末尾的元素
  • 返回值:删除的数组元素
  • 注意:修改原数组
  • 代码演示:
let text = fruits.pop()
console.log(text); // Banana
console.log(fruits); // ['apple', 'orange']

4. array.shift()

  • 方法含义:删除数组头部的元素
  • 返回值:删除的数组元素
  • 注意:修改原数组
  • 代码演示:
let text = fruits.shift()
console.log(text); // apple
console.log(fruits); // ['orange', 'Banana']

5.array.unshift()

  • 方法含义:在数组头部添加元素
  • 返回值:数组长度
  • 注意:修改原数组
  • 代码演示:
let text = fruits.unshift('Lemon')
console.log(text); // 4
console.log(fruits); // ['Lemon', 'apple', 'orange', 'Banana']

6. array.indexOf(item,start)

  • 方法含义:从头到尾地检索数组,返回数组中某个指定的元素位置。
  • 参数解释:
    • item:必须。查找的元素。
    • start:可选的整数参数。规定在数组中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
  • 返回值:数组元素的索引 / -1
  • 代码演示:
console.log(fruits.indexOf('apple')); // 0
console.log(fruits.indexOf('apple',1)); // -1

7. array.lastIndexOf(item,start)

  • 方法含义:可返回一个指定的元素在数组中最后出现的位置,从该字符串的后面向前查找。
  • 参数解释:
    • item:必须。查找的元素。
    • 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。
  • 返回值:数组元素的索引 / -1
  • 代码演示:
console.log(fruits.lastIndexOf('Banana')); // 2
console.log(fruits.lastIndexOf('Banana',1)); // -1

8.array.splice(index,length,item)

  • 方法含义:通过索引删除某个元素 / 向数组添加新项
  • 参数解释:
    • index:要添加的位置
    • length:删除的个数
    • item:添加的数组元素
  • 返回值:被删除的数组元素
  • 代码演示:
// 测试1:数组元素替换
let text = fruits.splice(1,2,'lemon','mango')
console.log(text); // ['orange', 'Banana']
console.log(fruits); // ['apple', 'lemon', 'mango']

// 测试2:删除数组元素 - 不省略length
let text = fruits.splice(1,2)
console.log(text); // ['orange', 'Banana']
console.log(fruits); // ['apple']

// 测试3:删除数组元素 - 省略length
let text = fruits.splice(1)
console.log(text); // ['orange', 'Banana']
console.log(fruits); // ['apple']

9. array.slice(startIndex,endIndex)

  • 方法含义:裁剪数组
  • 参数解释:
    • startIndex:要剪切的开始位置
    • endIndex:要剪切的结束位置(可省略,即剪切到末尾)
  • 返回值:剪切出来的数组
  • 代码演示:
// 测试1:不省略endIndex
let text = fruits.slice(1,2)
console.log(text); // ['orange']

// 测试2:省略endIndex
let text = fruits.slice(1)
console.log(text); // ['orange', 'Banana'] 

10. array.toString()

  • 方法含义:数组转化为字符串
  • 返回值:转化后的字符串
  • 代码演示:
let text = fruits.toString()
console.log(text); // apple,orange,Banana

11. array.reverse()

  • 方法含义:颠倒数组中元素的顺序。
  • 返回值:新数组
  • 注意:会修改原数组
  • 代码演示:
let fruits = ['apple', 'orange', 'Banana']
console.log(fruits.reverse()); //  ['Banana', 'orange', 'apple']
console.log(fruits); // ['Banana', 'orange', 'apple']

12. array.concat()

  • 方法含义:合并数组
  • 返回值:转化后的字符串
  • 代码演示:
// 测试1:合并两个数组
let text = fruits.concat(animals)
console.log(text); // ['apple', 'orange', 'Banana', 'dog', 'cat']

// 测试2:合并多个数组
let text = fruits.concat(animals,numbers)
console.log(text); // ['apple', 'orange', 'Banana', 'dog', 'cat', 'one', 'two']

// 测试3:将数组与值合并
let text = fruits.concat(['cake','hamburger'])
console.log(text); // ['apple', 'orange', 'Banana', 'cake', 'hamburger']

13. array.toString()

  • 方法含义:数组转化为字符串
  • 返回值:转化后的字符串
  • 代码演示:
let text = fruits.toString()
console.log(text); // apple,orange,Banana

14. Array.isArray()

  • 方法含义:确定传递的值是否是一个Array
  • 返回值:布尔值
  • 代码演示:
console.log(Array.isArray(fruits)); // true
console.log(Array.isArray(['apple', 'orange', 'Banana'])); // true
console.log(Array.isArray({name:'zs'})); // false

15. array.filter()

  • 方法含义:筛选出数组中符合条件的所有元素
  • 返回值:数组
  • 代码演示:
console.log(fruits.filter(item => item.length > 5)); 
// ['orange', 'Banana']

16. array.every()

  • 方法含义:检测数组所有元素是否都符合指定条件

    • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
    • 如果所有元素都满足条件,则返回 true。
  • 返回值:布尔值

  • 注意:every() 不会对空数组进行检测。

  • 代码演示:

console.log(fruits.every(item => item.length > 5)); // false
console.log(fruits.every(item => item.length > 4)); // true

17. array.find()

  • 方法含义:返回通过测试(函数内判断)的数组的第一个元素的值。

    • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
    • 如果没有符合条件的元素返回 undefined
  • 返回值:符合条件的第一个元素 / undefined

  • 注意:find()不会对空数组进行检测。

  • 代码演示:

console.log(fruits.find(item => item.length > 5)); // orange
console.log(fruits.find(item => item.length > 10)); // undefined

18. array.findIndex()

  • 方法含义:返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

    • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
    • 如果没有符合条件的元素返回 -1
  • 返回值:1 / -1

  • 注意:findIndex() 对于空数组,函数是不会执行的。

  • 代码演示:

console.log(fruits.findIndex(item => item.length > 5)); // 1
console.log(fruits.findIndex(item => item.length > 10)); // -1

18. array.includes(searchElement, fromIndex)

  • 方法含义:用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
  • 参数说明:
    • searchElement:必须。需要查找的元素值。
    • fromIndex:可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
  • 返回值:布尔值
  • 代码演示:
// 测试1:省略 fromIndex
console.log(fruits.includes('one')); // false
console.log(fruits.includes('apple')); // true

// 测试2:不省略 fromIndex
console.log(fruits.includes('apple',1)); // false
// fromIndex为负值,则按升序从 array.length(3) + fromIndex(-3) 的索引 0 开始搜索。
console.log(fruits.includes('apple',-3)); // true
// 如果计算出的索引小于 0,则整个数组都会被搜索。
console.log(fruits.includes('apple',-100)); // true

20. array.join(separator)

  • 方法含义:把数组中的所有元素转换一个字符串。
  • 参数说明:可选。指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
  • 返回值:返回一个字符串。
  • 代码演示:
console.log(fruits.join()); // apple,orange,Banana
console.log(fruits.join('/')); // apple/orange/Banana
console.log(fruits.join(' and ')); // apple and orange and Banana

21. array.map()

  • 方法含义:返回数组中的元素为原始数组元素调用函数处理后的值。
  • 返回值:返回一个新数组
  • 代码演示:
console.log(num.map(item => item + 2 )); // [3, 4, 5]

22. array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

  • 方法含义:接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
  • 参数说明:
    • function(total,currentValue, index,arr):必需。用于执行每个数组元素的函数。
      • total:必需。初始值, 或者计算结束后的返回值。
      • currentValue:必需。当前元素
      • currentIndex:可选。当前元素的索引
      • arr:可选。当前元素所属的数组对象。
    • initialValue:可选。传递给函数的初始值
  • 返回值:返回计算结果
  • 代码演示:
// 测试1:没有初始值initialValue
let sum = num.reduce(function (prev, cur, index, arr) {
      console.log(prev, cur, index);
      return prev + cur;
    },0)
console.log(arr, sum);

// 测试2:
let sum = num.reduce(function (prev, cur, index, arr) {
      console.log(prev, cur, index);
      return prev + cur;
    },0)
console.log(num, sum);

在这里插入图片描述
在这里插入图片描述
reduce()方法的具体说明

23. array.sort(sortfunction)

  • 方法含义:对数组的元素进行排序。排序顺序可以是字母或数字,并按升序或降序。
    默认排序顺序为按字母升序。
  • 参数说明:sortfunction:可选。规定排序顺序。必须是函数。
  • 返回值:
  • 代码演示:
let arr = [1,23,123,5]
// 测试1:不加参数
// 按照默认排序方式,按照字符串的Unicode码,所以会导致123在23的前面。
console.log(arr.sort()); //  [1, 123, 23, 5]

// 测试2:加参数
console.log(arr.sort(function(a,b){return a-b})); // [1, 5, 23, 123]
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值