Correct code. it prints in the console:
1
0
https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSCFJNHQ2k1hvNCD3F9zKp9WiPooSBkjUy-ymynu1T0kmiaW-7r
66
which is pretty correct, bacaouse the is only one search_objects item.
var j;
for (j=0;j< search_objects.length; j++){
console.log(j)
website = search_objects[j].website;
var rand = Math.floor(Math.random()*3)
var img_src;
switch (rand){
case 0: img_src = "res1.png"; break;
case 1: img_src = "res2.png"; break;
case 2: img_src = "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSCFJNHQ2k1hvNCD3F9zKp9WiPooSBkjUy-ymynu1T0kmiaW-7r"; break;
}
console.log(img_src);
var template_result = '<div class="owl-item " ..........div>'
console.log(search_objects[j].ID_subr)
add_carousel_item(template_result)
}
BUT if i change it to a for in loop it will executed one more time.
var j; //same thing if i declare like this: for(var j in search_objects)
for (j in search_objects){
console.log(j)
website = search_objects[j].website;
var rand = Math.floor(Math.random()*3)
var img_src;
switch (rand){
case 0: img_src = "res1.png"; break;
case 1: img_src = "res2.png"; break;
case 2: img_src = "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSCFJNHQ2k1hvNCD3F9zKp9WiPooSBkjUy-ymynu1T0kmiaW-7r"; break;
}
console.log(img_src);
var template_result = '<div class="owl-item " ..........div>'
console.log(search_objects[j].ID_subr)
add_carousel_item(template_result)
}
And this loop in this second case will be executed 2 time when search_objects has only one item and this is proved by printing to the console the objects itself and it's length.
while this is shown in console.
1 //length
0 //j
res1.png //img from math.random
66 //property of the the first item
unique // value of j ??????!!!!!!!!!!!!!
res2.png //img from math.random
undefined //seems to show property of the search_objects[unique]
Notice: this happens most of the times. not 100% of the times. let's say it dont happen when i exe it as the first thing i do after page is refreshed. Anyone have seen such kind of thing before?