Counting the occurrences / frequency of array elements

前端 未结 30 2133
甜味超标
甜味超标 2020-11-21 06:47

In Javascript, I\'m trying to take an initial array of number values and count the elements inside it. Ideally, the result would be two new arrays, the first specifying each

30条回答
  •  故里飘歌
    2020-11-21 07:39

    Here is a way to count occurrences inside an array of objects. It also places the first array's contents inside a new array to sort the values so that the order in the original array is not disrupted. Then a recursive function is used to go through each element and count the quantity property of each object inside the array.

    var big_array = [
      { name: "Pineapples", quantity: 3 },
      { name: "Pineapples", quantity: 1 },
      { name: "Bananas", quantity: 1 },
      { name: "Limes", quantity: 1 },
      { name: "Bananas", quantity: 1 },
      { name: "Pineapples", quantity: 2 },
      { name: "Pineapples", quantity: 1 },
      { name: "Bananas", quantity: 1 },
      { name: "Bananas", quantity: 1 },
      { name: "Bananas", quantity: 5 },
      { name: "Coconuts", quantity: 1 },
      { name: "Lemons", quantity: 2 },
      { name: "Oranges", quantity: 1 },
      { name: "Lemons", quantity: 1 },
      { name: "Limes", quantity: 1 },
      { name: "Grapefruit", quantity: 1 },
      { name: "Coconuts", quantity: 5 },
      { name: "Oranges", quantity: 6 }
    ];
    
    function countThem() {
      var names_array = [];
      for (var i = 0; i < big_array.length; i++) {
        names_array.push( Object.assign({}, big_array[i]) );
      }
    
      function outerHolder(item_array) {
        if (item_array.length > 0) {
          var occurrences = [];
          var counter = 0;
          var bgarlen = item_array.length;
          item_array.sort(function(a, b) { return (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0); });
    
          function recursiveCounter() {
            occurrences.push(item_array[0]);
            item_array.splice(0, 1);
            var last_occurrence_element = occurrences.length - 1;
            var last_occurrence_entry = occurrences[last_occurrence_element].name;
            var occur_counter = 0;
            var quantity_counter = 0;
            for (var i = 0; i < occurrences.length; i++) {
              if (occurrences[i].name === last_occurrence_entry) {
                occur_counter = occur_counter + 1;
                if (occur_counter === 1) {
                  quantity_counter = occurrences[i].quantity;
                } else {
                  quantity_counter = quantity_counter + occurrences[i].quantity;
                }
              }
            }
    
            if (occur_counter > 1) {
              var current_match = occurrences.length - 2;
              occurrences[current_match].quantity = quantity_counter;
              occurrences.splice(last_occurrence_element, 1);
            }
    
            counter = counter + 1;
    
            if (counter < bgarlen) {
              recursiveCounter();
            }
          }
    
          recursiveCounter();
    
          return occurrences;
        }
      }
      alert(JSON.stringify(outerHolder(names_array)));
    }
    

提交回复
热议问题