group array and get count

前端 未结 6 441
执念已碎
执念已碎 2021-01-12 07:00

Say I have array like this:

[
   \"foo\",
   \"bar\",
   \"foo\"
   \"bar\",
   \"bar\",
   \"bar\",
   \"zoom\"
]

I want to group it so I

相关标签:
6条回答
  • 2021-01-12 07:40

    Explanation: First, we create object (a brand new empty object). Secondly using the spread operator we first copy everything from the existing array and make a new array out of it and use that to create a new Set (the Set object lets you store unique values) and use that as the key, and finally we filter out our array (with a little help from the length property) to see how many times a particular key occurs and set that to the object's value. And remember, since we used Object.assign() and put everything inside of it, so what gets returned is an object containing the result of everything that I tried to explain (to the best of my knowledge) above :) We also used the comma operator here which simply evaluates each of its operands (from left to right) and returns the value of the last operand.

    const arr = ["foo", "bar", "foo", "bar", "bar", "bar", "zoom"];
    
    const data = Object.assign({}, ...Array.from(new Set(arr), key => ({[key]: arr.filter(value => value === key).length})));
    
    console.log(data);

    0 讨论(0)
  • 2021-01-12 07:41

    Yes you can reduce() your array to an object with the keys and the count, like this:

    const input = [
      "foo",
       "bar",
       "foo",
       "bar",
       "bar",
       "bar",
       "zoom"
    ];
    const result = input.reduce((total, value) => {
         total[value] = (total[value] || 0) + 1;
         return total;
    }, {});
    console.log(result);

    Hope it helps!

    0 讨论(0)
  • 2021-01-12 07:45

    Just use the function Array.prototype.reduce.

    let array = [   "foo",   "bar",   "foo",   "bar",   "bar",   "bar",   "zoom"],
        result = array.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), Object.create(null));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2021-01-12 07:52

    You could use the reduce() method which is avalible on the array object to achieve this grouping. So, something along the lines of this might achieve what you're after:

        var input = [
           "foo",
           "bar",
           "foo",
           "bar",
           "bar",
           "bar",
           "zoom"
        ]
        
        // Iterate the input list and construct a grouping via a "reducer"
        var output = input.reduce(function(grouping, item) {
        
          // If the current list item does not yet exist in grouping, set 
          // it's initial count to 1
          if( grouping[item] === undefined ) {    
            grouping[item] = 1;
          }
          // If current list item does exist in grouping, increment the 
          // count
          else {
            grouping[item] ++;
          }
        
          return grouping;
        
        }, {})
        
        console.log(output)

    0 讨论(0)
  • 2021-01-12 07:53

    You can do this in a concise way via reduce:

    var arr = [ "foo", "bar", "foo", "bar", "bar", "bar", "zoom" ] 
    
    var result = arr.reduce((r,c) => (r[c] = (r[c] || 0) + 1, r), {})
    
    console.log(result)

    It gets really cute if you ware to use lodash and _.countBy:

    var arr = [ "foo", "bar", "foo", "bar", "bar", "bar", "zoom" ] 
    
    var result = _.countBy(arr);
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

    0 讨论(0)
  • 2021-01-12 07:55

    Yeah I guess with Array.prototype.reduce, it's just:

      const map = list.reduce((a, b) => {
                a[b] = a[b] || 0;
                return ++a[b], a;
              }, {});
    

    wondering if there is less verbose way tho.

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