title: count all values on object array - javascript
body: <p>I need to count each value on  the object array , the desired output should be  like below </p>
  
  <pre><code>[{
      "question": "question1",
      "USA": 2
    }, {
      "question": "question1",
      "AUS": 1
    },
  
    {
      "question": "question2",
      "item1": 2
    },
  
    {
      "question": "question2",
      "item1,item2": 1
    }, {
      "question": "question4",
      "3": 1
    }, {
      "question": "question4",
      "2": 1
    }
  ]
  </code></pre>
  
  <p>Below is the input I need to transform in to the above output. I  have no clue how to do with n no of question and also got issue when one question has 2 answers . sample input</p>
  
  <pre><code>[{"question1":"USA","question2":["item1"],"question4":2}, 
  {"question1":"USA","question2":["item1"],"question4":3}, 
  {"question1":"AUS","question2":["item1","item2"]}];
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let arr=[{"question1":"USA","question2":["item1"],"question4":2},{"question1":"USA","question2":["item1"],"question4":3},{"question1":"AUS","question2":["item1","item2"]}];
  //console.log(arr);
  
  
  
  function solve(list){
      var map = new Map();
      var entry = null;
      for(var item of list){
          if(!map.has(item.question1))
              map.set(item.question1, {question:'question1'});
  
              entry = map.get(item.question1);
  
          if(entry.hasOwnProperty(item.question1))
              entry[item.question1] = entry[item.question1] + 1;
          else
              entry[item.question1] = 1;
              
           
            if(!map.has(item.question2))
              map.set(item.question2, {question: 'question2'});
  
              entry = map.get(item.question2);
  
          if(entry.hasOwnProperty(item.question2))
              entry[item.question2] = entry[item.question2] + 1;
          else
              entry[item.question2] = 1;
           
      }
      return Array.from(map.values());
  }
  console.log(solve(arr))</code></pre>
  </div>
  </div>
  </p>