数组
at()
at()
方法接收一个索引,并返回该索引对应的数组元素,允许正数和负数。负数表示从倒数第一个开始获取。
- 使用正数时,
array[0]
和array.at(0)
等价; - 使用负数时,
array[array.length - 1]
和array.at(-1)
等价;
基本示例:
js
const array = [5, 12, 8, 130, 44];
const res1 = array.at(2) // 8
const res2 = array.at(-1) // 44
concat()
concat()
方法用于合并两个或多个数组。该方法不会改变原数组。
基本示例:
js
const array1 = ["a", "b"]
const array2 = [4, 5, 6]
const array3 = array1.concat(array2) // ["a", "b", 4, 5, 6]
entries()
entries()
方法返回一个新的 数组迭代器 对象,该对象包含数组中每个元素的 [键, 值]
对。
基础示例:
js
const array = ["a", "b", "c", "d"]
const iterator = array.entries()
console.log(iterator.next().value) // [0, "a"]
console.log(iterator.next().value) // [1, "b"]
示例 2:
js
// for..of循环
for (const element of iterator) {
console.log(element)
}
every()
every()
方法用于测试 数组内的元素是否全部满足指定条件。若满足,则停止遍历并返回 true,若不满足,则返回 false。
注意,对于空数组,every()
方法永远返回 true。
基本示例:
js
const array = [8, 30, 39, 29, 10, 13]
const result1 = array.every(item => item < 40) // true
const result2 = array.every(item => item < 5) // false
const array2 = []
const resule3 = arr.every(item => item) // true
filter()
filter()
方法用于 过滤数组内满足条件的元素,若一条都不满足,则返回空数组。
基本使用:
js
const array = [8, 30, 39, 29, 10, 13]
const result1 = array.filter(item => item > 20) // [30, 30, 29]
示例 2:
js
const array = [
{ id: 15 },
{ id: true },
{ id: -100 },
{ id: "a" },
{},
{ id: null },
{ id: NaN },
{ id: undefined }
]
let count = 0
function filterById(item) {
if (item.id) return true
count++
return false
}
// 过滤出 id 为假的元素
const result = array.filter(filterById) // 只返回前 4 项
// 无效的数量
console.log(count) // 4
find()
find()
方法返回数组中满足条件的第一个元素的值,否则返回 undefined。该方法不会改变原数组。
基础示例:
js
const array = [5, 12, 8, 130, 44]
const result = array.find(item => item > 10) // 12
示例 2:
js
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "cherries", quantity: 5 }
]
function isCherries(fruit) {
return fruit.name === "cherries"
}
const res1 = inventory.find(item => isCherries(item)) // [{ name: "cherries", quantity: 5 }]
const res2 = inventory.find(isCherries) // [{ name: "cherries", quantity: 5 }]
示例 3:
js
// 箭头函数 + 解构
const res3 = inventory.find(({ name }) => name === "cherries") // [{ name: "cherries", quantity: 5 }]
findLast()
findLast()
与 find()
方法相反,从后往前查找第一个满足条件的元素,如果未找到则返回 undefined。该方法不会改变原数组。
基础示例:
js
const array = [5, 12, 50, 120, 33]
const res = array.findLast(item => item > 45) // 120
示例 2:
js
const inventory = [
{ name: "apples", quantity: 2 },
{ name: "bananas", quantity: 0 },
{ name: "fish", quantity: 1 },
{ name: "cherries", quantity: 5 }
]
function isNotEnough(item) {
return item.quantity < 2
}
const res1 = inventory.findLast(isNotEnough) // [{ name: "fish", quantity: 1 }]
示例 3:
js
// 箭头函数 + 解构
const res2 = inventory.findLast(({ quantity }) => quantity < 2) // [{ name: "fish", quantity: 1 }]
flat()
flat()
方法用于扁平化一个数组,深度递归的将所有子元素拼接到新数组。该方法不会改变原数组。
基础示例:
js
const array = [0, 1, [2, [3, [4, 5]]]]
console.log(array.flat()) // [0, 1, 2, [3, [4, 5]]]
console.log(array.flat(2)) // [0, 1, 2, 3, [4, 5]]
console.log(array.flat(Infinity)) //[0, 1, 2, 3, 4, 5]