回顾之路---数组方法

数组方法

arr.length
数组的长度。
不改变原数组,返回值是数组的长度。

const arr = [1, 2, 3, 4]
arr.length
// ====> arr.length === 4

arr.unshift(some)
在数组最前面插入 some 。
改变原数组,返回值是插入 some 后,数组的长度。

const arr = [1, 2, 3, 4]
arr.unshift(5)
// ===> arr = [5, 1, 2, 3, 4]
// ===> arr.unshift(5) === arr.length

arr.shift()
删除数组最前面一个元素。
改变原数组,返回值是被删除的元素。

const arr = [1, 2, 3, 4]
arr.shift()
// ====> arr = [2, 3, 4]
// ====> arr.shift() === 1

arr.push(some)
在数组的末尾添加 some。
改变原数组,返回值是添加 some 后数组的长度。

const arr = [1, 2, 3, 4]
arr.push(4)
// ====> arr = [1, 2, 3, 4, 4]
// ====> arr.push(4) === arr.length

arr.pop()
删除数组末尾的一个元素。
改变原数组,返回值是删除的元素。

const arr = [1, 2, 3, 4]
arr.pop()
// ====> arr = [1, 2, 3]
// ====> arr.pop() === 4

arr.join(operator)
将数组元素,用 operator 进行连接,生成字符串。
不改变原数组,返回值是生成的字符串。

const arr = [1, 2, 3, 4]
arr.join('&')
// ====> arr = [1, 2, 3, 4]
// ====> arr.join('&') === '1&2&3&4'

arr.reverse()
翻转数组,头变尾, 尾变头。
改变原数组,返回值是改变后的数组

cost arr = [1, 2, 3, 4]
arr.reverse()
// ====> arr = [4, 3, 2, 1]
// ====> arr.reverse() === arr

arr.sort()
数组排序,按照规则对数组元素进行排序,默认按照 ASCII 码值由小到大排序。
改变原数组,返回值是排序过的数组。

const arr = [1, 'a', '1', 'A']
arr.sort()
// ====> arr = [1, '2', 'A', 'a']
// ====> arr.sort() === arr
// 升序排序
arr.sort(function(a, b) {
	return a - b
})
// 降序排序
arr.sort(function(a, b) {
	return b - a
})

arr.concat()
数组拼接,将两个数组拼接。
不改变原数组,返回值是拼接后的数组。

const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]
arr1.concat(arr2)
// ====> arr1 = [1, 2, 3]
// ====> arr2 = [4, 5, 6]
// ====> arr1.concat(arr2) === [1, 2, 3, 4, 5, 6]

arr.slice(start, end)
数组截取,截取 从下标 start开始,截取到下标 end 结束。
不设置 end 会一直截取到最后一位。
start 设置负数, 代表从后往前数。
start 和 end 都不设置代表全部截取。
不改变原数组,返回值是截取部分。

const arr = [1, 2, 3, 4, 5, 6]
arr.slice(1, 2)
// [2, 3]
arr.slice(3)
// [4, 5, 6]
arr.slice(-4, 5)
// [3, 4, 5]
arr.slice(-2)
// [5, 6]
arr.slice()
// [1, 2, 3, 4, 5, 6]

arr.splice(start, length, items)
数组替换、删除、添加。start 位置开始, 删除 length 个长度的值, 并加入 items。
改变原数组,返回值是删除部分组成的数组。

const arr = [1, 2, 3, 4, 5, 6]
arr.splice(1, 1, 22)
// [2]
// arr ===> [1, 22, 3, 4, 5, 6]
arr.splice(1, 1)
// [2]
// arr ====> [1, 3, 4, 5, 6]
arr.splice(1, 0, 1.5)
// []
// arr ===> [1, 1.5, 2, 3, 4, 5, 6]

arr.indexOf(item, start)
从下标 start 开始查找元素 item 在数组中首次出现的位置。
start 可以不设置,代表从 0 开始查找。
不改变原数组,返回值是查找到元素的下标,未找到是 -1。

const arr = [1, 2, 3, 4, 5, 6]
arr.indexOf(1, 2)
// -1
arr.indexOf(5, 2)
// 4
arr.indexOf(1)
// 0
arr.indexOf(10)
// -1

arr.lastIndexOf(item, start)
从第 start 个元素开始往前查找 item 第一次出现的位置。
不改变原数组,返回值是查找到元素位置的下标,未找到是 -1。

const arr = [1, 2, 3, 4, 5, 6]
arr.lastIndexOf(3)
// 2
arr.lastIndexOf(10)
// -1
arr.lastIndexOf(4, 5)
// 3
arr.lastIndexOf(4, 1)
// -1

arr.forEach(function(item, index, arr){})
数组遍历,参数是一个回调函数。
回调函数有三个参数item, index,arr。
item:数组的每一项。
index: 每一项对应的下标。
arr:数组本身。
可以在遍历的时候修改item,原数组会发生变化。没有返回值。

const arr = [1, 2, 3, 4, 5]
arr.forEach((item, index, arr) => {
	console.log('forEach', item, index, arr)
})
// forEach, 1, 0, [1, 2, 3, 4, 5]
// forEach, 2, 1, [1, 2, 3, 4, 5]
// forEach, 3, 2, [1, 2, 3, 4, 5]
// forEach, 4, 3, [1, 2, 3, 4, 5]
// forEach, 5, 4, [1, 2, 3, 4, 5]
// 返回值 undefined

arr.map(function(item,index,arr){})
映射,参数是一个回调函数。
回调函数有三个参数item, index,arr。
item:数组的每一项。
index: 每一项对应的下标。
arr:数组本身。
回调函数要有return,最终的返回值是一个新的数组,新数组与原数组的长度相同。

const arr = [1, 2, 3, 4, 5, 6]
const map = arr.map((item, index, arr) => {
	console.log('map', item, index, arr)
	return item * 2
})
// map 1 0 (6) [1, 2, 3, 4, 5, 6]
// map 2 1 (6) [1, 2, 3, 4, 5, 6]
// map 3 2 (6) [1, 2, 3, 4, 5, 6]
// map 4 3 (6) [1, 2, 3, 4, 5, 6]
// map 5 4 (6) [1, 2, 3, 4, 5, 6]
// map 6 5 (6) [1, 2, 3, 4, 5, 6]
// 返回值 [2, 4, 6, 8, 10, 12]
const map1 = arr.map(item => {
	return {
		old: item,
		new: item * 2
	}
})
// 返回值 [{old: 1, new: 2}, {old: 2, new: 4}, ..., {old: 6, new: 12}]

未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值