Object对象和数组有很多内置方法

web前端开发

共 3023字,需浏览 7分钟

 ·

2021-10-17 21:56

来源 | http://www.fly63.com/


Object.keys(), Object.values()和Object.entries()

Object.keys()返回对象键Object.values()的数组,返回对象值Object.entries()的数组,并以格式返回对象的键和相应值的数组[key, value]。

const obj = {  a: 1 ,b: 2 ,c: 3}
console.log(Object.keys(obj)) // ['a', 'b', 'c']console.log(Object.values(obj)) // [1, 2, 3]console.log(Object.entries(obj)) // [['a', 1], ['b', 2], ['c', 3]]

Object.entries() 使用 for-of 循环和解构赋值

const obj = {  a: 1 ,b: 2 ,c: 3}
for (const [key, value] of Object.entries(obj)) { console.log(`key: ${key}, value: ${value}`)}

Object.entries()用for-of 循环和解构赋值来迭代结果非常方便。

For-of 循环可让您迭代数组元素。语法是for (const element of array)(我们可以const用var或替换let,但const如果我们不打算修改 ,最好使用element)。

解构赋值允许您从数组或对象中提取值并将它们分配给变量。在这种情况下,const [key, value]意味着不是将[key, value]数组分配给 ,而是将该数组element的第一个元素分配给key,将第二个元素分配给value。它相当于:

for (const element of Object.entries(obj)) {  const key = element[0]       ,value = element[1]}

如您所见,解构使这变得更加简单。

Array.prototype.every() 和 Array.prototype.some()

如果指定的回调函数为数组的每个元素every()返回,true则该方法返回。如果指定的回调函数为某个(至少一个)元素返回,则该方法返回。truesome()truetrue

const arr = [1, 2, 3]
// true, because every element is greater than 0console.log(arr.every(x => x > 0))// false, because 3^2 is greater than 5console.log(arr.every(x => Math.pow(x, 2) < 5))// true, because 2 is even (the remainder from dividing by 2 is 0)console.log(arr.some(x => x % 2 === 0))// false, because none of the elements is equal to 5console.log(arr.some(x => x === 5))

Array.prototype.find() 和 Array.prototype.filter()

这些find()方法返回满足提供的回调函数的第一个元素。该filter()方法返回满足提供的回调函数的所有元素的数组。

const arr = [1, 2, 3]
// 2, because 2^2 !== 2console.log(arr.find(x => x !== Math.pow(x, 2)))// 1, because it's the first elementconsole.log(arr.find(x => true))// undefined, because none of the elements equals 7console.log(arr.find(x => x === 7))
// [2, 3], because these elements are greater than 1console.log(arr.filter(x => x > 1))// [1, 2, 3], because the function returns true for all elementsconsole.log(arr.filter(x => true))// [], because none of the elements equals neither 6 nor 7console.log(arr.filter(x => x === 6 || x === 7))

Array.prototype.map()

该map()方法返回一个数组,其中包含对数组元素调用提供的回调函数的结果。

const arr = [1, 2, 3]
console.log(arr.map(x => x + 1)) // [2, 3, 4]console.log(arr.map(x => String.fromCharCode(96 + x))) // ['a', 'b', 'c']console.log(arr.map(x => x)) // [1, 2, 3] (no-op)console.log(arr.map(x => Math.pow(x, 2))) // [1, 4, 9]console.log(arr.map(String)) // ['1', '2', '3']

Array.prototype.reduce()

该reduce()方法通过调用提供的具有两个元素的回调函数将数组缩减为单个值。

const arr = [1, 2, 3]
// Sum of array elements.console.log(arr.reduce((a, b) => a + b)) // 6// The largest number in the array.console.log(arr.reduce((a, b) => a > b ? a : b)) // 3

该reduce()方法采用可选的第二个参数,即初始值。当您调用的数组reduce()可以有零个或一个元素时,这很有用。例如,如果我们想创建一个函数sum(),它接受一个数组作为参数并返回所有元素的总和,我们可以这样写:

const sum = arr => arr.reduce((a, b) => a + b, 0)
console.log(sum([])) // 0console.log(sum([4])) // 4console.log(sum([2, 5])) // 7


学习更多技能

请点击中国公众号

浏览 30
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报