Skip to main content
added 67 characters in body
Source Link
pomeh
  • 4.9k
  • 4
  • 26
  • 44

You should not use variable i as index for contArr variable, because each time you have a new event data, i is reseted to 0.

I would have done something like this:

var allContinents = {}; // this is an object, not an array

socket.on('data', function(data) {
    
    //iterate through the socket data
    data.forEach(function (item, i){
      //get current continent name
      var continent = item.continent;{

      if (!(item.continent in allContinents))
      {
        // we create the object if it doesn't exist yet
        // the object key is the contient name...
        allContinents[item.continent] = {
          continent: continent,
          val: 1,
        };
      }
      
      // ... so it's easy to retrieve the item using the same key
      allContinents[item.continent].val++;
      console.log(allContinents[item.continent].val);
      
    });
    
});

You should not use variable i as index for contArr variable, because each time you have a new event data, i is reseted to 0.

I would have done something like this:

var allContinents = {};

socket.on('data', function(data) {
    
    //iterate through the socket data
    data.forEach(function (item, i){
      //get current continent name
      var continent = item.continent;

      if (!(item.continent in allContinents))
      {
        // we create the object if it doesn't exist yet
        allContinents[item.continent] = {
          continent: continent,
          val: 1,
        };
      }
      
      allContinents[item.continent].val++;
      console.log(allContinents[item.continent].val);
      
    });
    
});

You should not use variable i as index for contArr variable, because each time you have a new event data, i is reseted to 0.

I would have done something like this:

var allContinents = {}; // this is an object, not an array

socket.on('data', function(data) {
    
    //iterate through the socket data
    data.forEach(function (item, i) {

      if (!(item.continent in allContinents))
      {
        // we create the object if it doesn't exist yet
        // the object key is the contient name...
        allContinents[item.continent] = {
          continent: continent,
          val: 1,
        };
      }
      
      // ... so it's easy to retrieve the item using the same key
      allContinents[item.continent].val++;
      console.log(allContinents[item.continent].val);
    });
    
});
Source Link
pomeh
  • 4.9k
  • 4
  • 26
  • 44

You should not use variable i as index for contArr variable, because each time you have a new event data, i is reseted to 0.

I would have done something like this:

var allContinents = {};

socket.on('data', function(data) {
    
    //iterate through the socket data
    data.forEach(function (item, i){
      //get current continent name
      var continent = item.continent;

      if (!(item.continent in allContinents))
      {
        // we create the object if it doesn't exist yet
        allContinents[item.continent] = {
          continent: continent,
          val: 1,
        };
      }
      
      allContinents[item.continent].val++;
      console.log(allContinents[item.continent].val);
      
    });
    
});