Count number of elements with certain properties inside JSON

前端 未结 5 1956
一生所求
一生所求 2020-12-16 23:28

I have some JSON data:

{\"humans\": [
    { \"firstName\" : \"Paul\", \"lastName\" : \"Taylor\", \"hairs\": 2 },
    { \"firstName\" : \"Sharon\", \"lastName         


        
相关标签:
5条回答
  • 2020-12-16 23:45

    javascript for loop is fastest:

    var counter = 0;
    
    for (var i = 0; i < data.humans.length; i++) {
       if (data.humans[i].hairs === 2) {
          counter++;
       }
    }
    
    0 讨论(0)
  • 2020-12-16 23:47

    here's an example abstracting the grouping function and getting a list style result:

    var data = {
                "humans": [
                    { "firstName" : "Paul", "lastName" : "Taylor", "hairs": 2 },
                    { "firstName" : "Sharon", "lastName" : "Mohan", "hairs": 3 },
                    { "firstName" : "Mohan", "lastName" : "Harris", "hairs": 3 },
                    { "firstName" : "Deborah", "lastName" : "Goldman", "hairs": 4 },
                    { "firstName" : "Mark", "lastName" : "Young", "hairs": 4 },
                    { "firstName" : "Tom", "lastName" : "Perez", "hairs": 4 },
                    { "firstName" : "Joseph", "lastName" : "Goldman", "hairs": 5 },
                    { "firstName" : "Mary", "lastName" : "White", "hairs": 5 },
                    { "firstName" : "Matthew", "lastName" : "Garcia", "hairs": 5 },
                    { "firstName" : "Patricia", "lastName" : "Allen", "hairs": 5 },
                    { "firstName" : "Larry", "lastName" : "Robinson", "hairs": 6 },
                    { "firstName" : "Manb", "lastName" : "Lopez", "hairs": 6 },
                    { "firstName" : "Jose", "lastName" : "Martinez", "hairs": 6 },
                    { "firstName" : "Deborah", "lastName" : "Walker", "hairs": 6 },
                    { "firstName" : "Joseph", "lastName" : "Lopez", "hairs": 6 },
                    { "firstName" : "Tinman", "lastName" : "Moore", "hairs": 7 },
                    { "firstName" : "Jose", "lastName" : "Jackson", "hairs": 7 },
                    { "firstName" : "Karen", "lastName" : "Goldman", "hairs": 7 },
                    { "firstName" : "Paul", "lastName" : "Taylor", "hairs": 7 },
                    { "firstName" : "Amy", "lastName" : "Gonzalez", "hairs": 7 },
                    { "firstName" : "Richard", "lastName" : "Martinez", "hairs": 7 }
                ]
            };
    
    function groupByHair(array, groupOf) {
     var groups = {};
      array.forEach(function(element) {
      var groupName = groupOf(element);
       if (groupName in groups)
         groups[groupName].push(element);
       else
         groups[groupName] = [element];
      });
      return groups;
    }
    
    var byHairs = groupByHair(data.humans , function(h) {
      return h.hairs;
     });
    
    for (var hairs in byHairs) {
     var hairsN = byHairs[hairs].length;
     console.log('in '+ hairs +'hairs-group you have: ' +hairsN+' people');
    }
    

    fiddle

    0 讨论(0)
  • 2020-12-16 23:49

    You can use the filter function to filter an array of objects :

    var data = {...}
    
    data.humans.filter(function(o) { return o.hairs == 2 }).length
    //Return the number of humans who have 2 hairs
    

    Take a look fiddle

    0 讨论(0)
  • 2020-12-16 23:59

    Can you try this ? It will group your data and you will get count for same.

    var groupedData = _.groupBy(YourJsonName, function(d){return d.hairs});
    
    0 讨论(0)
  • 2020-12-17 00:02

    My solution may be what you want

    var data = {
                    "humans": [
                        { "firstName" : "Paul", "lastName" : "Taylor", "hairs": 2 },
                        { "firstName" : "Sharon", "lastName" : "Mohan", "hairs": 3 },
                        { "firstName" : "Mohan", "lastName" : "Harris", "hairs": 3 },
                        { "firstName" : "Deborah", "lastName" : "Goldman", "hairs": 4 },
                        { "firstName" : "Mark", "lastName" : "Young", "hairs": 4 },
                        { "firstName" : "Tom", "lastName" : "Perez", "hairs": 4 },
                        { "firstName" : "Joseph", "lastName" : "Goldman", "hairs": 5 },
                        { "firstName" : "Mary", "lastName" : "White", "hairs": 5 },
                        { "firstName" : "Matthew", "lastName" : "Garcia", "hairs": 5 },
                        { "firstName" : "Patricia", "lastName" : "Allen", "hairs": 5 },
                        { "firstName" : "Larry", "lastName" : "Robinson", "hairs": 6 },
                        { "firstName" : "Manb", "lastName" : "Lopez", "hairs": 6 },
                        { "firstName" : "Jose", "lastName" : "Martinez", "hairs": 6 },
                        { "firstName" : "Deborah", "lastName" : "Walker", "hairs": 6 },
                        { "firstName" : "Joseph", "lastName" : "Lopez", "hairs": 6 },
                        { "firstName" : "Tinman", "lastName" : "Moore", "hairs": 7 },
                        { "firstName" : "Jose", "lastName" : "Jackson", "hairs": 7 },
                        { "firstName" : "Karen", "lastName" : "Goldman", "hairs": 7 },
                        { "firstName" : "Paul", "lastName" : "Taylor", "hairs": 7 },
                        { "firstName" : "Amy", "lastName" : "Gonzalez", "hairs": 7 },
                        { "firstName" : "Richard", "lastName" : "Martinez", "hairs": 7 }
                    ]
                };
    var counterList = [];
    $.each(data.humans,function(i,item){
        counterList.push(item.hairs);
    });
    
    $.extend({
        distinct : function(anArray) {
           var result = [];
           $.each(anArray, function(i,v){
               if ($.inArray(v, result) == -1) result.push(v);
           });
           return result;
        }
    });
    
    var uniqueCounterList= $.distinct(counterList);
    var html = "";
    $.each(uniqueCounterList,function(j,itemUnique){
        html += "<div>Hair "+itemUnique+": "+(data.humans.filter(function(o) { return o.hairs == itemUnique }).length)+"</div>";
    });
    $("#count").html(html);
    

    Or http://jsfiddle.net/8g5ggfeh/

    0 讨论(0)
提交回复
热议问题