Skip to content
Advertisement

How to count the JSON object and on the basis of count take the same output

  1. How to count the JSON object and on the basis of count take the same output

        var obj = 
       [
        {"id":"0","name":"Mike Johnson","group":1},
        {"id":"1","name":"Bob Smith","group":2},
        {"id":"2","name":"Richard Thomas","group":3},
        {"id":"3","name":"Betty White","group":16},
        {"id":"4","name":"Tim Thompson","group":3},
        {"id":"5","name":"Carl Lewis","group":16},
        {"id":"6","name":"Kathy Towers","group":3},
        {"id":"7","name":"Billy Bob","group":1},
        {"id":"8","name":"Sally Bailey","group":1}
        ];
    
    1. First I would like the count after it on the basis of count. I want same output like input. for Count:-

      var count = 0;
      function getCount() {
      
      for (var i = 0; i < obj.length; i++) {
      
              count++;
      
      }
      return count;
      }
      

    for output :-

    function showDetails()   this is not giving the proper output
    { 
    
       for(var j=0; j< count; j++){ 
       obj.push([{j}]);    
       }                   
    alert(obj.name);        
    }                      
    alert(showDetails());   
    And I want an output like:- 
    
         var obj = 
       [
        {"id":"0","name":"Mike Johnson","group":1},
        {"id":"1","name":"Bob Smith","group":2},
        {"id":"2","name":"Richard Thomas","group":3},
        {"id":"3","name":"Betty White","group":16},
        {"id":"4","name":"Tim Thompson","group":3},
        {"id":"5","name":"Carl Lewis","group":16},
        {"id":"6","name":"Kathy Towers","group":3},
        {"id":"7","name":"Billy Bob","group":1},
        {"id":"8","name":"Sally Bailey","group":1}
        ];
    

    Can anybody help me please?

Advertisement

Answer

var str = "January,February,March,April,May,June,July,August,September,October";

var arr = str.split(',').map(function(v) {
 return {name: v};
});

console.log(arr);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement