티스토리 뷰

HTML&CSS&Javscript&Jquery

190819 reduce

猫猫 2019. 8. 23. 17:29
반응형
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        var a = [100,8,9,45,10,20];
        var result = a.reduce(function(arr,j,k){
            console.log("first para : ",arr, "second para: ",j,"third para: ",k);
            //result
            //first para :  7 second para:  8 third para: 1
            //first para :  15 second para:  9 third para: 2
            return arr-=j; //reduce는 처음을 기점으로 계속 마이너스 해나감.마치..토너먼트?
                         //1차원 배열->하나의 값(스칼라)
                         //2차원 배열->1차원배열로
        });
        console.log(result);
        //result 
        //24
    </script>
</head>
<body>
    
</body>
</html>
반응형