• js 对象数组 根据对象中的元素去重


    <script type="text/javascript">
    //根据 sys_time 和 loc_time 去重
    
        var tmp = 
            [
                {
                    "id":1,
                    "sys_time": "2015-03-09 15:15:08",  //与id为2的重复
                    "loc_time": "2015-03-09 15:16:38"
                }, 
                {
                    "id":2,
                    "sys_time": "2015-03-09 15:15:08", 
                    "loc_time": "2015-03-09 15:15:38",    //与id为3的重复(但是因为sys_time 已经和id为1的重复了 , 实际上不会再参与 loc_time的去重)
                }, 
                {
                    "id":3,
                    "sys_time": "2015-03-09 15:16:08", 
                    "loc_time": "2015-03-09 15:15:38", 
                    
                },
                {
                    "id":4,
                    "sys_time": "2015-03-09 15:17:08", 
                    "loc_time": "2015-03-09 15:17:38", 
                }
            ]
            
            
        // for(n in tmp){
        //     tmp[n].sys_time = new Date(tmp[n].sys_time).getTime();
        // }
        
        var result = [], hash = {};  hash_ = {}
        for (var i = 0; i<tmp.length; i++) {
            var elem = tmp[i].sys_time; 
            var elem_ = tmp[i].loc_time; 
            if (!hash[elem]) {
                if (!hash_[elem_]) {
                    result.push(tmp[i]);
                    hash_[elem_] = true;
                };
                hash[elem] = true;
            }
        }
        
        console.log(result)
    
    </script>

    结果:

    [
        {
            "id":1,
            "sys_time": "2015-03-09 15:15:08",
            "loc_time": "2015-03-09 15:16:38"
        }, 
        {
            "id":3,
            "sys_time": "2015-03-09 15:16:08", 
            "loc_time": "2015-03-09 15:15:38", 
            
        },
        {
            "id":4,
            "sys_time": "2015-03-09 15:17:08", 
            "loc_time": "2015-03-09 15:17:38", 
        }
    ]
  • 相关阅读:
    最长不重复子串
    add two nums
    logistic 回归
    threesum
    KNN算法思想与实现
    Python的易错点
    ccf 目录格式转换
    Azure 带宽
    Office 365 如何使用powershell查询邮件追踪
    Azure AD Connect 手动同步
  • 原文地址:https://www.cnblogs.com/MDK-L/p/4588424.html
Copyright © 2020-2023  润新知