JS高级-day04-数组方法05-09

5. find

作用

寻找数组中满足条件的一个元素

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

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

        // find 返回符合 条件的数组元素。
        const arr = [
            {username: '香香', height: 80},
            {username: '妲己', height: 60},
            {username: '貂蝉', height: 70},
            {username: '大乔', height: 50},
            {username: '小乔', height: 30}
        ];
        // 要求的 返回   身高是 30的 那一个对象

        // 1. 
        // let obj;
        // arr.forEach((value) => {
        //   if (value.height === 30) {
        //     // 找到了
        //     obj = value;
        //     return
        //   }
        //   console.log(value);
        // });
        // console.log(obj);
        
        // 2. 
        // const obj = arr.find((value) => {
        //   console.log(value);
        //   if (value.height === 30) {
        //     return true;
        //   } else {
        //     return false;
        //   }
        // });
        
        // 3. 
        const obj = arr.find(value => value.height === 30)
        console.log(obj);
    </script>

6. findindex

作用

寻找数组中满足条件的元素 的对应的下标

没有找到 就返回 -1

<script>
        // findIndex  符合条件的元素的下标!!
        // 用法可以find很类似  在函数中 如果找到了 返回true
        // 找不到 就返回 -1 

        const arr = [
            {username: '香香', height: 80},
            {username: '妲己', height: 60},
            {username: '貂蝉', height: 70},
            {username: '大乔', height: 50},
            {username: '小乔', height: 30}
        ];

        // 帮我找到  身高 30 的元素 并且删掉它整个对象
        const index = arr.findIndex(value => value.height === 30)
        // 删除
        arr.splice(index, 1)
        // 打印数组
        console.log(arr);
        // 打印删除的下标
        console.log(index);
    </script>

7. includes

作用

判断数组中有没有包含值 包含 返回 true 否则返回 false

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

        // 判断数组中是否包含‘b’ 返回 true 或 false
        const newArr = arr.includes('b') //true
        console.log(newArr);
    </script>

8. join

作用

  1. 数组转成字符串
  2. 传递了参数 参数 将数组的每个元素 连接 !!!
    <script>
        // join方法  负责把  数组  转成  字符串 
        // join 含义 加入
        const arr = ['a', 'b', 'c', 'd'].map(value => `<li>${value}</li>`)
        // 本身  数组
        // 0: "<li>a</li>"
        // 1: "<li>b</li>"
        // 2: "<li>c</li>"
        // 3: "<li>d</li>"
        // length: 4
        console.log(arr);

        // 转化 字符串
        const result = arr.join('')
        // <li>a</li><li>b</li><li>c</li><li>d</li>
        console.log(result);
    </script>

9. indexOf

作用

判断数组中有没有包含这个元素 有 返回元素下标

没有 则返回 -1

    <script>
        // 类似 findIdex
        // indexOf 搜索数组中的元素,并且返回 它所在的 位置
        // 找到了  就返回元素的下标
        // 没有找到  就返回  -1
        const arr = ['a', 'b', 'c', 'd']

        // 有没有包含 字母 c 
        const index = arr.indexOf('c')  // 包含返回 下标   不包含返回 -1

        console.log(index); // 2
    </script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. join() 功能: 将数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的,默认使用’,'号分...
    樑丶阅读 105评论 0 0
  • 数组的创建方式1.字面量的形式:var arr=[1,2,3];1.构造函数:var arr1=new Array...
    飘曳_87de阅读 244评论 0 0
  • 1、 join join,就是把数组转换成字符串,然后给他规定个连接字符,默认的是逗号( ,)。不改变原数组。 ...
    时间的溺水者阅读 253评论 0 0
  • 本文目录 数组的遍历1.1 for in1.2 for..of 循环1.3 for 循环1.4 array.for...
    前端辉羽阅读 680评论 0 16
  • es6 filter() 数组过滤方法总结1.创建一个数组,判断数组中是否存在某个值 2.也可以通过上面方法过滤掉...
    正在成长的切图仔阅读 249评论 0 2