1. 程式人生 > >JavaScript利用物件去重複值

JavaScript利用物件去重複值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Object Test2</title>
    <script type="text/javascript" charset="utf-8">
        
        function unique(arr) {
            var obj = {};
            for(var i=0; i<arr.length; i++){
                obj[arr[i]] = arr[i];
            }
            document.write(obj + '<br>');

            var res = [];
            debugger;
            for(var attribute in obj){
                res.push(obj[attribute]);
            }
            return res;
        }

        document.write(unique([1,2,3,4,2,4,5,44,3,555]));

    </script>
</head>
<body>

</body>
</html>