• json二位数组根据指定键名去重


    有时候json二维数组需要根据键名去重,这时候就可以用下面这个函数了,可以指定键名。

    //json去重函数
    //1、传入json或数组
    //2、传入去重的键名
    function abs(arr,key_name){
        var new_arr = [];
        var i = 0;
        for(key in arr){
            var key_name_value = arr[key][key_name];
            var is_key_name_value = false;
            for(key1 in arr){
                if(key>key1 && key_name_value==arr[key1][key_name]){
                    is_key_name_value = true;
                    break;
                }
            }
            if(is_key_name_value===false){
                new_arr[i] = arr[key];
                i++;
            }
        }
        return new_arr;
    }
    
    //定义json
    var a = [{user_id: "WV00000002", user_name: "王维", work_date: "2020-08-06", class_id: 9, classes_name: "颜色测试"},
    {user_id: "WV00000002", user_name: "王维", work_date: "2020-08-14", class_id: 1, classes_name: "早班"}
    ,{user_id: "WV00000004", user_name: "张颖", work_date: "2020-08-09", class_id: 9, classes_name: "颜色测试"},{user_id: "WV00000005", user_name: "王发通", work_date: "2020-08-15", class_id: 9, classes_name: "颜色测试"},{user_id: "WV00000007", user_name: "宋叶飞", work_date: "2020-08-06", class_id: 9, classes_name: "颜色测试"}];
    
    //调用json去重
    var result = abs(a,'class_id');
    
    //打印json
    console.log(result);
  • 相关阅读:
    java web 初学
    学习2
    学习
    上课
    Java中字母大小写的转换
    心得体会
    servlet请求
    响应设置消息体
    servlet响应-头部信息的设置
    servlet的响应(一)
  • 原文地址:https://www.cnblogs.com/phpyangbo/p/13425275.html
Copyright © 2020-2023  润新知