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

7b5d8cc6f60f5903b22ab216f8c96004.png

来源 | 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 0
console.log(arr.every(x => x > 0))
// false, because 3^2 is greater than 5
console.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 5
console.log(arr.some(x => x === 5))

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

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

 
 
const arr = [1, 2, 3]


// 2, because 2^2 !== 2
console.log(arr.find(x => x !== Math.pow(x, 2)))
// 1, because it's the first element
console.log(arr.find(x => true))
// undefined, because none of the elements equals 7
console.log(arr.find(x => x === 7))


// [2, 3], because these elements are greater than 1
console.log(arr.filter(x => x > 1))
// [1, 2, 3], because the function returns true for all elements
console.log(arr.filter(x => true))
// [], because none of the elements equals neither 6 nor 7
console.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([]))     // 0
console.log(sum([4]))    // 4
console.log(sum([2, 5])) // 7

学习更多技能

请点击中国公众号

6d89df35d2485c8e756a58c83738230d.gif

fda6ad81df6ae2b9ad08294727b76bc5.png

f60658c940956982bae9e11e884fba24.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值