JavaScript数组方法

原文链接: https://mp.weixin.qq.com/s/4ucLmUEHVZm8gQ6oAQpPNA

1.some()

此方法对数组中每一个元素与测试元素匹配,如果该函数满足任一项返回true,否则返回false。
some() 不会对空数组进行检测;some() 不会改变原始数组。

const myAwesomeArray = ["a", "b", "c", "d", "e"]
let a = myAwesomeArray.some(test => test === "d")
console.log(a) //true

2.every()

every()是对数组中每一项运行给定函数,如果该函数所有项返回true,则返回true。一旦有一项不满足则返回flase.

const myAwesomeArray = ["a", "b", "c", "d", "e"]
let a = myAwesomeArray.every(test => test === "d")
console.log(a) //false
const myAwesomeArray1 = ["a", "a", "a", "a", "a"]
let b = myAwesomeArray1.every(test => test === "a")
console.log(b) //true

3.reduce()

此方法接收一个函数作为累加器。它为数组中的每个元素依次执行回调函数,不包括数组中被删除或者从未被赋值的元素。函数应用于累加器,数组中的每个值最后只返回一个值。
reduce() 方法接受四个参数:初始值(上一次回调的返回值),当前元素值,当前索引,原数组。

语法:arr.reduce(callback,[initialValue])

callback:函数中包含四个参数
- previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
- currentValue (数组中当前被处理的元素)
- index (当前元素在数组中的索引)
- array (调用的数组)

- initialValue (作为第一次调用 callback 的第一个参数。)
//利用reduce来计算数组总和
const arr = [1, 2, 3, 4, 5]
const sum = arr.reduce((pre, item) => {
    return pre + item
}, 0)
console.log(sum) // 15
//利用reduce来计算一个字符串中每个字母出现次数
const str = 'abbaccdefazfs';
const obj = str.split('').reduce((pre, item) => {
	pre[item] ? pre[item]++ : pre[item] = 1
	return pre
}, {})
console.log(obj) // {a: 3, b: 2, c: 2, d: 1, e: 1,f: 2,s: 1,z: 1 }

4、map()

该方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。它按照原始数组元素顺序依次处理元素。
map() 不会对空数组进行检测;map() 不会改变原始数组。

const arr = [4, 9, 16, 25]
let a = arr.map(Math.sqrt)
console.log(a) //[2, 3, 4, 5]

5、forEach()

此方法用于调用数组的每个元素。并将元素传递给回调函数。
forEach() 对于空数组是不会执行回调函数的。

const arr = [4, 9, 16, 25]
arr.forEach((item,index)=>{
	console.log(item+'--'+index)
})
//4--0
//9--1
//16--2
//25--3

6、flat()

此方法创建一个新数组,其中包含子数组上的holden元素,并将其平整到新数组中。请注意,此方法只能进行一个级别的深度。

[1, 2, [3, 4]].flat() // [1, 2, 3, 4]
[1, 2, [3, [4, 5]]].flat()// [1, 2, 3, [4, 5]]
[1, 2, [3, [4, 5]]].flat(2)// [1, 2, 3, 4, 5]
//不管有多少层嵌套,都要转成一维数组,可以用Infinity关键字作为参数
[1, 2, [3, [4, 5]]].flat(Infinity)// [1, 2, 3, 4, 5]
//原数组有空位,flat()方法会跳过空位
[1, 2, , 4, 5].flat()// [1, 2, 4, 5]

7、filter()

filter()方法使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组。
filter() 不会对空数组进行检测,也不会改变原始数组

语法:array.filter(function(currentValue,index,arr), thisValue)
//去掉数组中不符合项
 var filtered = [12, 5, 8, 12, 44].filter(item=>item>=7); 
 console.log(filtered);//[ 12, 8, 12, 44 ]
//数组去重
var arr = [2, 2, 3, 4, 5, 5, 6, 7, 5, 6, 7,8,2,1];
var arr2 = arr.filter((x, index,arr1)=>arr1.indexOf(x)===index) 
console.log(arr2); // [2, 3, 4, 5, 6, 7, 8, 1]

8、findIndex()

此方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。它为数组中的每个元素都调用一次函数执行,当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。如果没有符合条件的元素返回 -1
findIndex() 对于空数组,函数是不会执行的, findIndex() 并没有改变数组的原始值。

const arr = [  { id: 1, name: "john" }, { id: 2, name: "Ali" },  { id: 3, name: "Mass" },]
let a = arr.findIndex(element => element.id === 3)//2
let a1 = arr.findIndex(element => element.id === 4)//-1

9、find()

此方法返回通过测试(函数内判断)的数组的第一个元素的值。find() 方法为数组中的每个元素都调用一次函数执行:当数组中的元素在测试条件时回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。如果没有符合条件的元素返回 undefined。
find() 对于空数组,函数是不会执行的;find() 并没有改变数组的原始值。

const arr = [  { id: 1, name: "john" }, { id: 2, name: "Ali" },  { id: 3, name: "Mass" },]
let a = arr.find(element => element.id === 3)//{id: 3, name: "Mass"} 
let a1 = arr.find(element => element.id === 4)//undefined

10、sort()

此方法接收一个函数作为参数。它对数组的元素进行排序并返回它。也可以使用含有参数的sort()方法进行排序。

const arr = [12, 5, 8, 12, 44]
arr.sort()//[12, 12, 44, 5, 8]
arr.sort((a, b) => a - b)//[5, 8, 12, 12, 44]
arr.sort((a, b) => b-a) //[44, 12, 12, 8, 5]

11、concat()

此方法用于连接两个或多个数组/值,它不会改变现有的数组。而仅仅返回被连接数组的一个新数组。

const arr = [12, 4, 8, 12, 44],arr1=[4, 9, 16, 25]
arr.concat(arr1)//[12, 4, 8, 12, 44, 4, 9, 16, 25]

12、fill()

此方法的作用是使用一个固定值来替换数组中的元素。该固定值可以是字母、数字、字符串、数组等等。它还有两个可选参数,表示填充起来的开始位置(默认为0)与结束位置(默认为array.length)。
fill() 方法用于将一个固定值替换数组的元素。array.fill(value, start, end)

//array.fill(value, start, end)
const arr = [1, 2, 3, 4, 5]
arr.fill(1)//[1, 1, 1, 1, 1]
arr.fill(0, 1, 3)//[1, 0, 0, 4, 5] 

13、includes()

此方法用于判断字符串是否包含指定的子字符串。如果找到匹配的字符串则返回 true,否则返回 false。
includes() 方法区分大小写。

const arr = [12, 4, 8, 12, 44]
arr.includes(12)//true
arr.includes(1)//false

14、reverse()

此方法用于颠倒数组中元素的顺序。第一个元素成为最后一个,最后一个元素将成为第一个。

const arr = ["e", "d", "c", "b", "a"]
arr.reverse()// ['a', 'b', 'c', 'd', 'e']

15、flatMap()

该方法将函数应用于数组的每个元素,然后将结果压缩为一个新数组。它在一个函数中结合了flat()和map()。

const arr  = [[1], [2], [3], [4], [5]]
arr.flatMap(item=> item* 10)//[10, 20, 30, 40, 50]
//等价于
arr.flat().map(item => item* 10)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值