js高级 数组方法

数组方法- find 返回符合 条件的数组元素

  <script>

  // find 用找满足条件的数组中一个元素 
  // 找到了之后 不会再继续往下遍历 
  // 代码中 找到了 你需要返回true 

  // forEach 也可以做遍历 但是 不能被中断 打断 (for循环不一样!! )

      
      
      
      // forEach 做遍历的时候 是不能被打断 中断  break!!!!
      // for 和 foreach有什么区别  1 都是循环  2 for循环可以被中断 但是 foreach不可以!!

      // find 返回符合 条件的数组元素。
      const arr = [
        { username: '悟空', height: 70 },
        { username: '八戒', height: 60 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
      ];
      // // 要求的 返回   身高是 60的 那一个对象
      // let obj;
      // arr.forEach((value) => {
      //   if (value.height === 60) {
      //     // 找到了
      //     obj = value;
      //     return
      //   }
      //   console.log(value);
      // });

      // console.log(obj);

      // const obj = arr.find((value) => {
      //   console.log(value);
      //   if (value.height === 60) {
      //     return true;
      //   } else {
      //     return false;
      //   }

      // });

      const obj = arr.find((value) => value.height === 60);

      console.log(obj);
    </script>

数组方法-findIndex

<script>
      // findIndex  符合条件的元素的下标!!
      // 用法可以find很类似  在函数中 如果找到了 返回true
      const arr = [
        { username: '悟空', height: 70 },
        { username: '八戒', height: 60 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
        { username: '龙马', height: 30 },
      ];

      // 帮我找到 身高为60的那一个元素
      const index = arr.findIndex((value) => value.height === 660);
      
      console.log(index);

      // 帮我删除它!!
      // arr.splice(index,1);

      // console.log(arr);
    </script>

数组方法-includes()

 <script>
        //includes()判断一个数组是否包含一个指定的值
        const arr = [`a`, `b`, `c`, `d`]

        //判断数组中是否包含`b` 有返回true 没有false
        const result = arr.includes(`b`)
        console.log(result) 
    </script>

数组方法-indexOf

  <script>
        //类似 findIndex
        //indexOf 搜索数组中的元素,并返回它的所在位置
        //找到了 就返回元素的下标
        //未找到 返回 -1

        const arr = [`a`, `b`, `c`, `d`]
        //有没有包含之母b
        const index = arr.indexOf(`e`)

        console.log(index)
    </script>

数值方法案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text">
    <ul></ul>
    <script>
        /*
        需求 在输入框输入内容 按下回车键 
        把输入框内容 显示到列表中
        且去重
        */
        arr = [`苹果`, `香蕉`]
        const ul = document.querySelector(`ul`)
        const input = document.querySelector(`input`)
        renderHTML()
        window.addEventListener(`keyup`, e => {
            console.log(e.key)
            if (e.key == `Enter` && !arr.includes(input.value)) {
                //includes()判断一个数组是否包含一个指定的值
                arr.push(input.value)
                renderHTML()
            }
        })

        function renderHTML() {
            ul.innerHTML = arr.map(value => `<li>${value}</li>`).join(` `)

        }

    </script>
</body>

</html>

数组方法-join

  <script>
        //join 方法 负责把数组 转成字符串
        //join 含义 加入
        const arr = [`a`, `b`, `c`, `d`]
        //const arr = [`<li>a</li>`, `<li>b</li>`, `<li>c</li>`, `<li>d</li>`]
        const result = arr.join(``)
        console.log(result)
    </script>

Set对象

 <script>
      /* 
      1 Set 是一个对象  存放数据  数据永远不会重复 
      2 Set 当成是一个数组 
      3 Set 是一个对象  遍历 使用 数组方法 find findIndex map
         把Set对象转成 真正数组
         数组转成 set 对象
          const set = new Set([1,2,3,4]);


      4 小结
        1 Set 是一个对象 不会存放重复数据
        2 数组转成 set对象  const set =   new Set([])
        3 set对象 转成 数组  const arr=[...set]
        4 set对象 添加数据 使用add方法
          set.add(1)
          set.add(2)
          set.add(3)
       */

      //  存在旧的数组
      const list = [1, 4, 5, 6, 7];

      //  1 Set对象 需要被 new 出来使用
      const set = new Set(list);

      // 2 存放数据  调用 add方法
      set.add(1);
      set.add(2);
      set.add(2);
      set.add(2);
      set.add(2);
      set.add(2);
      set.add(2);
      set.add(3);

      // console.log(set);
      // 把set对象 转成数组
      const arr = [...set];
      console.log(arr);
    </script>

创建对象的n种方法

 <script>
      // 1 创建对象的方式  字面量 => 字面意思  (你是个好人)
      // 不方便维护 - 修改
      // const obj = { nickname: '八戒', height: 190 };
      // const obj1 = { nickname: '八戒', height: 190 };
      // const obj2 = { nickname: '八戒', height: 190 };
      // const obj3 = { nickname: '八戒', height: 190 };
      // const obj4 = { nickname: '八戒', height: 190 };
      // const obj5 = { username: '八戒', height: 190 };

      // 2 工厂函数 封装 继承!  
      function p(name,a,b,c,d,e) {
        return {
          nickname:name,
          a,b,c,d,e
        }
        
      }
      function person(name, height,a,b,c,d,e) {
        return {
          nickname: name,
          height,
          a,b,c,d,e
        };
      }

      const obj = person('八戒', 190);
      const obj1 = person('八戒', 190);
      const obj2 = person('八戒', 190);
      const obj3 = person('八戒', 190);
      console.log(obj);
      console.log(obj1);
      console.log(obj2);
      console.log(obj3);

      // 3 重点介绍 构造函数!!!!   
    </script>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容

  • 数组是 js 中最常用到的数据集合,其内置的方法有很多,熟练掌握这些方法,可以有效的提高我们的工作效率,同时对我们...
    魂斗罗小黑阅读 297评论 0 1
  • 数组方法备忘单: 添加/删除元素:push(...items) —— 向尾端添加元素,pop() —— 从尾端提取...
    个人观察日记阅读 306评论 0 0
  • 数组的排序 sort()方法排序问题。 sort()方法是Array原型链上自带的方法。 默认排序顺序是根据字符串...
    无迹落花阅读 592评论 1 0
  • 本文会先介绍所有数组方法,再详细介绍其中的reduce(引申阅读:redux中的compose函数),接着介绍in...
    zpkzpk阅读 409评论 0 1
  • 1、 join join,就是把数组转换成字符串,然后给他规定个连接字符,默认的是逗号( ,)。不改变原数组。 ...
    时间的溺水者阅读 232评论 0 0