数组高阶用法

数组的高阶函数forEach(),map(),filter(),some(),every()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [1,24,456,888,3456]
        arr.forEach(r =>{
            console.log(r);
            
        })//foreach没有返回值

		  console.log(arr.map(r => r*2)); //map有返回值,可以返回一些具有特定规律的值,比如原数组*2,每个元素加几,也可以返回一个对象,如下
		 let arr1 = [
            {
                name:'Sandy',
                age: 18
            },
            {
                name:'Jandy',
                age: 19
            },
            {
                name:'Simba',
                age: 38
            }
        ]
        console.log(arr1.map(r => r.name));
        console.log(arr1.map(r => r.age));



         console.log(arr.filter(r => r%2===0)) //筛选符合条件的元素
		 arr = arr.filter(r => r>400) 

        
		console.log(arr.some(r => r>400));//some查找元素只要有一个符合条件,结果返回true
        console.log(arr.every(r => r>400));//every查找元素只要有一个不符合条件,结果返回false
        

         console.log(arr);
         
         


    </script>
</body>
</html>

你可能感兴趣的:(数组高阶用法)