代码之家  ›  专栏  ›  技术社区  ›  Bharat

对JSON数据进行排序并获取前n条记录

  •  0
  • Bharat  · 技术社区  · 6 年前

    我该怎么办?

    这是我到目前为止所做的。

    //my origional json, it's too big but adding some portion of it.
    var jsonData = [    
      {
        "id": "5",
        "name": "#5"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "2",
        "name": "#2"
      },
      {
        "id": "8",
        "name": "#8"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "10",
        "name": "#10" 
      },  
      {
        "id": "2",
        "name": "#2"
      }];    
    
    var top10Data = [];
    
    //geting top 10 items
    function getTop10Data() {
        var i = 0;
        while (i <= 20) {
            top10Data.push(getTop1Data());
            i++;
        }
    
        return true;
    }
    
    //getting top 1 data that has max count in json
    function getTop1Data() {
        var store = jsonData, distribution = {}, max = 0, result = [];
    
        store.forEach(function (a) {
            distribution[a] = (distribution[a] || 0) + 1;
            if (distribution[a] > max) {
                max = distribution[a];
                result = [a];
                return;
            }
            if (distribution[a] === max) {
                result.push(a);
            }
        });
    
        //remove this item with it's all occurences, and push it to top10Data
        removeData(result); 
    
        return result;
    }
    
    //remove items from origional json. but this is not working properly as it removes only one item from top
    function removeData(result) {
        var length = jsonData.length;
        for (var i = 0; i < length; i++) {
            if (jsonData[i].toppings === result[0].toppings) {
                jsonData.splice(jsonData[i], 1);            
            }
        }
    }
    

    我的问题。

    我认为我的方式不合适,有没有更好的方法来处理这种情况。如果我的方法是正确的,那么我在当前代码中缺少了什么。

    任何帮助都将不胜感激。

    2 回复  |  直到 6 年前
        1
  •  1
  •   CertainPerformance    6 年前

    sort 将条目按其出现次数排序,然后对前10个条目进行切片。

    var jsonData = [    
      {
        "id": "5",
        "name": "#5"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "2",
        "name": "#2"
      },
      {
        "id": "8",
        "name": "#8"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "10",
        "name": "#10" 
      },  
      {
        "id": "2",
        "name": "#2"
      }];
     const counts = jsonData.reduce((a, obj) => {
      const string = JSON.stringify(obj);
      a[string] = (a[string] || 0) + 1
      return a;
    }, {});
    const result = Object.entries(counts)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 10)
      .map(([string, count]) => ({ count, obj: JSON.parse(string) }));
    console.log(result);

    counts

    var jsonData = [    
      {
        "id": "5",
        "name": "#5"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "2",
        "name": "#2"
      },
      {
        "id": "8",
        "name": "#8"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "10",
        "name": "#10" 
      },  
      {
        "id": "2",
        "name": "#2"
      }];
     const counts = jsonData.reduce((a, obj) => {
      const string = JSON.stringify(obj);
      a[string] = (a[string] || 0) + 1
      return a;
    }, {});
    jsonData.forEach((item) => {
      item.count = counts[JSON.stringify(item)];
    });
    console.log(jsonData);
        2
  •  1
  •   Pranbir Sarkar    6 年前

    我创造了一个逻辑,它是工作。 步骤如下:

    • 读取排序后的数组并计算出现的名称数 连续计数并存储计数
    • 根据计数的数目重新排序

    var jsonData = [    
      {
        "id": "5",
        "name": "#5"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "2",
        "name": "#2"
      },
      {
        "id": "8",
        "name": "#8"
      },
      {
        "id": "1",
        "name": "#1"
      },
      {
        "id": "10",
        "name": "#10" 
      },  
      {
        "id": "2",
        "name": "#2"
      }];    
       
      
      
      $('#output1').html(JSON.stringify(jsonData));
      
    	jsonData.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
      
      $('#output2').html(JSON.stringify(jsonData));
      
      let newArray= [];
      
      	let total = 1;
    for(let i=0;i<jsonData.length;i++){
     let nextName = (i==jsonData.length -1)?0 :	jsonData[i+1].name;
        let currentName = jsonData[i].name;
        
        if(nextName != currentName){
          newArray.push({
          	id : jsonData[i].id,
            name : currentName,
            count : total
          });
    				total = 1;
          }
          else{
          	total+=1;
          }
      }
      
      $('#output3').html(JSON.stringify(newArray));
      
      //Lets sort it again based on count and take the top 10
      
    newArray.sort((a, b) => a.count > b.count ? -1 : a.count < b.count ? 1 : 0)
    
    newArray = newArray.slice(0, 10); // Here is your Data
    
    $('#output4').html(JSON.stringify(newArray));
      
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <p id="output1">
    
    </p>
    <p id="output2">
    
    </p>
    <p id="output3">
    
    </p>
    <p id="output4">
    
    </p>