JavaScript数组操作 filter 方法示例 对比find方法

    xiaoxiao2023-10-04  163

    var 新数组 = (返回一个新数组) 数组.filter((数组元素)={return (根据条件判断的T/F结果)})

    //https://www.runoob.com/jsref/jsref-filter.html var ages = [32, 33, 19, 16, 18, 40]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.filter(checkAdult); // [ 32, 33, 19, 18, 40 ] } // 对比 find 函数 // https://www.runoob.com/jsref/jsref-find.html function myFunction1() { document.getElementById("demo").innerHTML = ages.find(checkAdult); // 32 , //“当数组中的元素在测试条件时返回 true 时, // find() 返回符合条件的元素,之后的值不会再调用执行函数。” }
    最新回复(0)