jsonArray 根据key 进行排序 去重

这篇博客介绍了如何使用JavaScript实现JSON数组的去重功能,具体方法是遍历数组并比较指定key的值,同时提供了按照时间字段排序的函数,利用Date对象比较时间戳来确保排序的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function uniqueArray(array, key){
    var result = [array[0]];
    for(var i = 1; i < array.length; i++){
        var item = array[i];
        var repeat = false;
        for (var j = 0; j < result.length; j++) {
            if (item[key] == result[j][key]) {
                repeat = true;
                break;
            }
        }
        if (!repeat) {
            result.push(item);
        }
    }
    //result 按照时间排序
     result.sort(sortId);
    return result;
}


  function sortId(a,b){
var oDate1 = new Date(a.Time);
var oDate2 = new Date(b.Time);
   if(oDate1.getTime() > oDate2.getTime()){
  return 1; 
    }
   else if(oDate1.getTime() == oDate2.getTime()){
   return 0;   
      }
   else{
   return -1;     
     }
 }
JSONObject map2 = jsonObject.getJSONObject(“map”); // 提取 x 轴数据 List xList2 = map2.getJSONArray(“x”).toJavaList(String.class); // 提取 y 数据 JSONArray yArray = map2.getJSONArray(“y”); // 按年份分组数据 Map<String, Map<String, Map<String, Double>>> yearDataMap = new LinkedHashMap<>(); for (Object obj : yArray) { JSONObject yItem = (JSONObject) obj; String name = yItem.getString(“name”); String[] parts = name.split(“😊; if (parts.length < 2) continue; String yearMonth = parts[0]; String metric = parts[1]; String year = yearMonth.substring(0, 4); // 提取年份 if (metric.equals(“CUR_REAL_ANP_ADJ”)) continue; List ratios = yItem.getJSONArray(“ratio”).toJavaList(String.class); // 初始化年份数据结构 yearDataMap.putIfAbsent(year, new LinkedHashMap<>()); for (int i = 0; i < xList2.size(); i++) { String xValue = xList2.get(i); if (xValue.equals(“空”) || xValue.equals(”-99") || xValue.equals(“-100”) || xValue.equals(“没开启”)) { continue; } String dimValue = xValue + prefix; String valueStr = ratios.get(i); // 初始化维度值数据结构 yearDataMap.get(year).putIfAbsent(dimValue, new LinkedHashMap<>()); // 转换并存储值 try { double value = Double.parseDouble(valueStr); yearDataMap.get(year).get(dimValue).put(metric, value); } catch (NumberFormatException e) { // 处理转换错误 yearDataMap.get(year).get(dimValue).put(metric, 0.0); } } } // 构建结果 JSONObject result2 = new JSONObject(); result2.put(“dimName”, jsonObject.getString(“dimName”)); result2.put(“title”, jsonObject.getString(“title”)); JSONArray dataArray2 = new JSONArray(); // 处理每个年份的数据 for (Map.Entry<String, Map<String, Map<String, Double>>> yearEntry : yearDataMap.entrySet()) { JSONObject yearObj = new JSONObject(); yearObj.put(“year”, yearEntry.getKey()); JSONArray objArray = new JSONArray(); for (Map.Entry<String, Map<String, Double>> dimEntry : yearEntry.getValue().entrySet()) { JSONObject dimObj = new JSONObject(); dimObj.put(“dimValue”, dimEntry.getKey()); // 添加所有指标值 for (Map.Entry<String, Double> metricEntry : dimEntry.getValue().entrySet()) { dimObj.put(metricEntry.getKey(), metricEntry.getValue()); } objArray.add(dimObj); } yearObj.put(“obj”, objArray); dataArray2.add(yearObj); } result2.put(“data”, dataArray2); // 构建请求参数 paramObj = buildRequestParams(Collections.singletonMap(“question”, result2.toString())); break; 在这段代码的基础上,把数据结构变为下面这种 { “dimName”:“车龄”,“title”:“提升效果”,“data”:[ { “year”:“2024”,“obj”:[ {“dimValue”:“新车车龄”,list[0.676,0.6832,0.0688]},{“dimValue”:“(0.75,1]车龄”,list:[0.6388,0.6978,0.0342]},{“dimValue”:“(1,2]车龄”,list:[0.6819,0.7279,0.0623]},{“dimValue”:“(2,3]车龄”,list:[0.6593,0.7286,0.0668]},{“dimValue”:“(3,4]车龄”,list:[0.6345,0.7024,0.0766]},{“dimValue”:“(4,5]车龄”,list:[0.7203,0.6853,0.0566]},{“dimValue”:“(5,6]车龄”,list:[0.6357,0.6636,0.0732]},{“dimValue”:“(6,7]车龄”,list:[0.6402,0.6532,0.081]},{“dimValue”:“(7,8]车龄”,list:[0.6382,0.6417,0.0979]},{“dimValue”:“(8,9]车龄”,list:[0.5962,0.6362,0.077]}, {“dimValue”:“(9,10]车龄”,list:[0.6465,0.6335,0.0766]},{“dimValue”:“(10,11]车龄”,list:[0.6215,0.6245,0.0592]},{“dimValue”:“(11,12]车龄”,list:[0.621,0.6197,0.0529]},{“dimValue”:“(12,13]车龄”,list:[0.5749,0.5831,0.0368]},{“dimValue”:“(13,14]车龄”,list:[0.5395,0.5792,0.032]},{“dimValue”:“(14,15]车龄”,list:[0.5657,0.5741,0.0229]},{“dimValue”:“>15车龄”,list:[0.513,0.5442,0.0245]} ] }, { “year”:“2025”,“obj”:[ {“dimValue”:“新车车龄”,list[0.6866,0.6936,0.0757]},{“dimValue”:“(0.75,1]车龄”,list:[0.609,0.6849,0.0499]},{“dimValue”:“(1,2]车龄”,list:[0.5467,0.6885,0.0619]},{“dimValue”:“(2,3]车龄”,list:[0.8135,0.6994,0.0648]},{“dimValue”:“(3,4]车龄”,list:[0.5713,0.6824,0.0693]},{“dimValue”:“(4,5]车龄”,list:[0.5425,0.6673,0.0627]},{“dimValue”:“(5,6]车龄”,list:[0.6058,0.6524,0.0736]},{“dimValue”:“(6,7]车龄”,list:[0.7198,0.6411,0.0742]},{“dimValue”:“(7,8]车龄”,list:[0.6564,0.631,0.0762]},{“dimValue”:“(8,9]车龄”,list:[0.6165,0.6211,0.0796]}, {“dimValue”:“(9,10]车龄”,list:[0.6178,0.6206,0.0713]},{“dimValue”:“(10,11]车龄”,list:[0.6791,0.6058,0.0634]},{“dimValue”:“(11,12]车龄”,list:[0.6719,0.6018,0.0504]},{“dimValue”:“(12,13]车龄”,list:[0.5914,0.5709,0.0411]},{“dimValue”:“(13,14]车龄”,list:[0.409,0.5634,0.0301]},{“dimValue”:“(14,15]车龄”,list:[0.6937,0.5616,0.0233]},{“dimValue”:“>15车龄”,list:[0.4288,0.5368,0.0317]} ] } ] }
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值