json去重

来源:互联网 发布:网络正常网页无法访问 编辑:程序博客网 时间:2024/05/01 18:43
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>json去重</title>    <style></style>    <script>        //json去重,主要思想就是 两重循环,遍历是否存在某个属性值        var tem = [{"code":"001"},{"code":"002"},{"code":"001"},{"code":"003"}];        Array.prototype.removeRepeat = function(){            var res = [this[0]];            for(var i = 1 ; i< this.length; i++){                var repeat = false;                for(var j=0; j<res.length; j++){                    if(this[i].code == res[j].code){                        repeat = true;                    }                }                if(!repeat){                    res.push(this[i]);                }            }            return res;        };        var result = tem.removeRepeat();        for(var j=0;j<result.length;j++){            console.log(result[j]);        }    </script></head><body>    this</body></html> 
0 0
原创粉丝点击