Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript for loop random return
    primarykey
    data
    text
    <p>I have this small source of code</p> <pre><code>for (i = 0; i &lt; elements.length; i++) { console.log(i); (function (i) { if (elements[i]["id"] == id) { index = i; console.log(i); } })(i); } console.log(index); </code></pre> <p>I want to find an array's element index where sub element "id" = id; but it returns sometimes 0 simetimes 1. This code is inside a function that is inside a class. and I call the same function twice but with different parameters outside of the class, so first it must show 0 and than 1, but sometimes it shows 1 and than 0. so can you help me? I tried this method too but it doesn't work.</p> <pre><code>var i = 0; while (i &lt; elements.length) { console.log(elements[i]["id"]); if (elements[i]["id"] == id) { index = i; break; } else if (elements[i]["id"] != id) { i++; } } console.log(index); </code></pre> <p><a href="http://www.myserver.net63.net/" rel="nofollow"><strong>Here is my test page</strong></a>, If you open it with Chrome (sometimes it happens but in Firefox it happens mostly) you will see that sometimes small image appears but sometimes it disapears when you press the button. You can also see the console output in "inspect elements -> console"</p> <p><strong>EDIT</strong></p> <p>I changed my code for better debug like this (as Lando suggested) :</p> <pre><code>for (i in elements){ console.log("comparing elements['"+ i +"']['id']: "+ elements[i]["id"] +" with id:' "+ id + "'"); if(elements[i]["id"] == id){ index = i; } } console.log(index); </code></pre> <p>and got console ouput:</p> <pre><code>comparing elements['0']['id']: id2 with id:' id1' Slideshow.js:140 comparing elements['1']['id']: id1 with id:' id1' Slideshow.js:140 1 Slideshow.js:147 comparing elements['0']['id']: id2 with id:' id2' Slideshow.js:140 comparing elements['1']['id']: id1 with id:' id2' Slideshow.js:140 0 </code></pre> <p>When it should be:</p> <pre><code>comparing elements['0']['id']: id1 with id:' id1' Slideshow.js:140 comparing elements['1']['id']: id2 with id:' id1' Slideshow.js:140 0 Slideshow.js:147 comparing elements['0']['id']: id1 with id:' id2' Slideshow.js:140 comparing elements['1']['id']: id2 with id:' id2' Slideshow.js:140 1 </code></pre> <p><strong>EDIT 2</strong></p> <p>After seeing the output it turns out that objects in array "elements" change places, <code>elements[0]["id"]</code> becames <code>"id2"</code> where it should be <code>"id1"</code> I can't still difinetly say why is this happening or what to do to fix it. so please if you have any ideas share it.</p> <p><a href="http://jsfiddle.net/PxfM8/" rel="nofollow">Here is my JSfiddle link</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload