zoukankan      html  css  js  c++  java
  • js_Array类型_find和findIndex

    • 这是ES6新增的方法

    find

    find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined。

        <p id="demo"></p><!--显示18-->
        <button onclick="myFunction()">click</button>
        <script>
            var ages=[3,10,18,20];
            function myFunction(){
                document.getElementById("demo").innerHTML=ages.find((item,index,array)=>{
                    return item>=18;
                });
            }
        </script>
    

    findIndex

    findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
    find为数组中每一个元素都调用一次函数执行:

    • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
    • 如果没有符合条件的元素返回 -1
      code:array.findIndex(function(item,index,array),this值);
        <p>点击按钮获取数组中年龄大于 18 的第一个元素索引位置。</p>
        <button onclick="myFunction()">点我</button>
        <p id="demo"></p><!--显示2-->
        <p><strong>注意:</strong> IE 11 及更早版本不支持 findIndex() 方法。</p>
        <script>
            var ages = [3, 10, 18, 20];
    
            function myFunction(){
                document.getElementById("demo").innerHTML=ages.findIndex((item,index,array)=>{
                    return item>=18;
                })
            }
            //array.findIndex(function(item,index,array))
        </script>
    
  • 相关阅读:
    nightwatchjs --Expect element to not include text
    Iterating elements using NightWatchJS
    nightwatch 切换窗口
    nodejs读取配置文件
    spring 事务
    重载,重写,重构
    python 元组不变 列表可变
    WebStorm ES6 语法支持设置
    docker日志
    curl -O 下载文件
  • 原文地址:https://www.cnblogs.com/Syinho/p/12404082.html
Copyright © 2011-2022 走看看