js中,find,filter,map,some,every,reduce等等的使用和使用场景

js中,find,filter,map,some,every,reduce等等的使用和使用场景

最近记忆力下降处理数据的时候老容易用错方法,特此写这篇文章。

📚 一、Array.prototype.find()

✅ 作用:

从数组中找出第一个符合条件的元素(满足回调函数返回 true),只返回一个元素

array.find(function(element, index, array) {
   
     // 返回 true 或 false })

📤 返回值:

  • 找到的第一个符合条件的元素(对象或基本类型)。
  • 如果没有找到,返回 undefined

🔍 示例:

js深色版本

const users = [
  {
   
    id: 1, name: 'Alice' },
  {
   
    id: 2, name: 'Bob' },
  {
   
    id: 3, name: 'Charlie' }
];

const user = users.find(u => u.id === 2);
console.log(user)
转载请说明出处内容投诉
CSS教程网 » js中,find,filter,map,some,every,reduce等等的使用和使用场景

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买