Help with Array

Help with Array
0

#1

Hello,

I need some help with the array of objectsfrom twitch tv -> https://gist.github.com/QuincyLarson/2ff6892f948d0b7118a99264fd9c1ce8
I can’t understand this behaviour. I assign the array to a variable called canales.
I’m trying to go over the array for extracting every channel (stream object). So I’m running this loop

  for (var i=0; i< data.length; i++){
    console.log(typeof(data[i]['stream'].name));
    console.log(data[i]['stream'].name, i);
    var html = '';
    html += '<div class="col-xs-12 col-md-6 col-lg-4">';
    html += '<div class="canal" id="canal' + data[i]['stream'].name + '">';

}
The console is telling me that I’ve a string, the name is freecodecamp and it’s the first element(0). Correct. But, I don’t know why I’m getting also this error:
TypeError: data[i].stream is null[Learn More] index.html:161:21

Why I’m receiving a string and at the same it’s telling me that it’s null?
Thanks


#2

Can you post your code, preferably in a CodePen or some other online editor?


#3

Here it is:


#4

The next element after “freecodecamp” is null. “stream” property doesn’t contain any values there.


#5

Yes, of course, but what I can’t understand, it’s why the loop is not continuing, I mean,

The first element name is freecodecamp.
The second is null.
The loop is not continuing, but should continue with the third element that is
esl_sc2
The fourth is null
The fifth is also null.


#6

That’s because of an error due to the elements being null - “TypeError: data[i].stream is null”.
You need to check if the value’s null before doing things to prevent such behavior.

[details=Hint]
var test1;
if (test1) // false
console.log(“not null!!”);
else
console.log(“null!!”);

var test2 = 123;
if (test2) // true
    console.log("not null!!");
else
    console.log("null!!");

Source: http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript[/details]