0

I've got the following array:

Object { id: "1", name: "a", hour: "08:00:00" }  
Object { id: "2", name: "b", hour: "08:00:00" }  
Object { id: "3", name: "c", hour: "08:00:00" }  
Object { id: "4", name: "d", hour: "07:15:00" }  
Object { id: "5", name: "e", hour: "08:00:00" }  
Object { id: "6", name: "f", hour: "08:00:00" }  
Object { id: "7", name: "g", hour: "09:00:00" }  
Object { id: "8", name: "h", hour: "08:30:00" }

And when I try to access the "hour" value by:

for(i = 0; i < array.length; i++){
    var hour = array[i]['hour'];
    console.log(hour);
}

I get the following result:

08:00:00  (3 repeats) 
07:15:00   
08:00:00  (2 repeats) 
09:00:00   
08:30:00

How can I avoid that and get:

08:00:00  
08:00:00 
08:00:00 
07:15:00   
08:00:00
08:00:00  
09:00:00   
08:30:00
Ivan Velchev
  • 75
  • 1
  • 1
  • 9

3 Answers3

4

You are seeing what browser is printing... You can't change this with js. It's a Browser dependent setting, in Chrome just enable timestamp to see each console.log in different lines.

Chrome debugger - how to turn off console.log message grouping?

Community
  • 1
  • 1
  • The problem is I use the value of "hour" to compare it with another value hold by "name" and what actually happens is that i compares the first 5 values with the values of the first output I've shown above. I am using Firefox. – Ivan Velchev May 16 '17 at 12:55
  • In Firefx this method seems to not work: http://stackoverflow.com/questions/40991956/how-do-i-disable-firefox-console-from-grouping-duplicate-output probably you can try as suggested to add the print message to an object and to print the object at the end of the cycle – Gian Luca Cecchi May 16 '17 at 13:01
0

That's browser specific. But you can do a workaround for this by saving your data in a variable where you seperate each line by a \n and make the output to the console at the end.

   var output='';
   for(i = 0; i < array.length; i++){
        var hour = array[i]['hour'];
        if (i) output +='\n';
        output += hour;
    }
    console.log(output);
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

Check that the console has different methods and usages.. but you can get the desired output in console by iterating over the array, using the index and calling a WindowOrWorkerGlobalScope.setTimeout() with the optional delay parameter index * 200:

var array = [{ id: "1", name: "a", hour: "08:00:00" }  , { id: "2", name: "b", hour: "08:00:00" }  , { id: "3", name: "c", hour: "08:00:00" }  , { id: "4", name: "d", hour: "07:15:00" }  , { id: "5", name: "e", hour: "08:00:00" }  , { id: "6", name: "f", hour: "08:00:00" }  , { id: "7", name: "g", hour: "09:00:00" }  , { id: "8", name: "h", hour: "08:30:00" }];

array.forEach(function (elem, index) {
  setTimeout(function () {
    console.log(elem.hour);
  }, index * 200);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46