如何使用Javascript对数据进行排序并根据键值测量长度

时间:2016-03-19 03:42:52

标签: javascript jquery

我需要一个帮助。我有一些数据需要按键值排序并测量长度。我正在解释下面的代码。

var response = [{
      day_id:1,
      day_name:"Monday",
      subcat_id:"2",
      cat_id:"1",
      comment:"hii"
}, {
      day_id:1,
      day_name:"Monday",
      subcat_id:"1",
      cat_id:"2",
      comment:"hello"
}
 {
      day_id:2,
      day_name:"Tuesday",
      subcat_id:"3",
      cat_id:"2",
      comment:"hii"
}]

此处day_id=1有两组数据,且day_id=2存在相同的数据。我需要根据day_id e,g.for day_id=1 length is 2来衡量数据集的长度。请帮助我

2 个答案:

答案 0 :(得分:2)

您可以map public static void main(String[] args) { System.out.println("\t\tProgram for Finding Prime Numbers: "); System.out.println("\t\t==================================="); Scanner input = new Scanner(System.in); while (true) { System.out.print("Please enter the first number: "); int num1 = input.nextInt(); System.out.print("Please enter the second number: "); int num2 = input.nextInt(); int lo = Math.min(num1, num2); int hi = Math.max(num1, num2); for (int i = lo; i <= hi; i++) { if (isPalindrome(i) && isPrime(i)) { System.out.println(i); } } System.out.print("Would you like to enter two more numbers, " + "Enter 1 for Yes or 0 for No: "); int choice = input.nextInt(); if (choice == 0) { break; } } } 并保存在对象中每次 id 的次数:

response

您的结果位于var counter = {}; response.map(function(item) { counter[item.day_id] = (counter[item.day_id] || 0) + 1; });

counter

希望它有所帮助。

答案 1 :(得分:0)

请勿使用if(resultnode != null) { resultnode.Value; // value } ,请使用.map,因为它不会返回另一个数组

并且代码可以更清洁一点:

.forEach