美文网首页
json数组合并去除重复数据案例

json数组合并去除重复数据案例

作者: 放下手机出来嗨 | 来源:发表于2019-06-05 16:37 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>json数组合并去重</title>
        </head>
        <body>
            <div id="txt"></div>
            <script>
                var a=[{"id":"1001","name":"张三","age":"18","address":"北京市朝阳区","school":"朝阳区第二中学"},{"id":"1002","name":"李四","age":"15","address":"北京市海淀区","school":"海淀区第二中学"},{"id":"1003","name":"王五","age":"16","address":"北京市石景山区","school":"石景山区第二中学"}];
                var b=[{"id":"1004","name":"小毛","age":"18","address":"北京市朝阳区","school":"朝阳区第二中学"},{"id":"1003","name":"王五","age":"16","address":"北京市石景山区","school":"石景山区第二中学"}]
                var c = a.concat(b); //合并数组
                var temp = {}; //存放id
                var result = []; //新数组
                c.map((item,index) => {
                    if(!temp[item.id]){
                        result.push(item);
                        temp[item.id] = true;
                    }
                })
                document.getElementById('txt').innerHTML = JSON.stringify(result)
            </script>
        </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:json数组合并去除重复数据案例

          本文链接:https://www.haomeiwen.com/subject/qnflxctx.html