当前位置:网站首页>JS array method review

JS array method review

2021-08-08 02:08:36 XFlyaa

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    let arr = [,1,2,3,,22];
    console.log(arr)

    // js  Traversal array 
    arr.forEach((item)=>{
    
        console.log(item);
    })

    arr.forEach(function (){
    
        console.log(this) //  the second arr The parameter points to this
    },arr)

    arr.find((item)=>{
    
        console.log(item);
    })

    //  Returns a new array   The original array remains the same 
    let ary = [1,2,'a',1,2]
    let ary1 = arr.filter((item,index)=>{
    
        //  Match the Numbers 
        // return /\d+/.test(item);
        return typeof item == 'number';
    })
    console.log(ary1)

    //  return bool  value   As long as one is true Namely true
    console.log(ary.some((item) => {
    
        // return false
        return typeof item == 'number';
    }));

    //  return bool  value   As long as there is one false for false
    console.log(ary.every((item) => {
    
        // return false
        return typeof item == 'number';
    }));

    let a1 = [1,2,3,4,5];
    // prev  representative  return Value 
    console.log(a1.reduce((prev, item) => {
    
        // console.log(prev, item);
        return prev + item;
    }));

</script>
</html>

版权声明
本文为[XFlyaa]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210808020608680M.html

随机推荐