Javascript: foreach skipping the first index (0)
<p>A somewhat tilted question which I didn't manage to figure out.</p>

<p>I'd like to generate an object from two arrays and give them keys, like this:</p>

<pre><code> var A = [1, 2, 3, 4]; var B = [7, 8, 9, 19];

 [ { "x": 1, "y": 7 }, { "x": 2, "y": 8 }, { "x": 3, "y": 9 }, { "x":
     4, "y": 19 } ]
</code></pre>

<p>I wrote the following code:</p>

<pre><code>var myData = []; var A = [1, 2, 3, 4]; var B = [7, 8, 9, 19];

A.forEach( function (item, index) {     
myData.push( { x: A[item], y: B[item] });   
});
</code></pre>

<p>But the output of it was </p>

<pre><code>[ { "x": 2, "y": 8 }, { "x": 3, "y": 9 }, { "x": 4, "y": 19 }, {} ]
</code></pre>

<p>By putting in [item - 1] in the function, it works as I'd like it to, but I can't understand why as the w3School example that I looked at seems very straightforward and works from index-0:</p>

<p><a href="http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_foreach" rel="noreferrer">http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_foreach</a></p>

<p>My Fiddle example: <a href="https://jsfiddle.net/53p5b3w8/5/" rel="noreferrer">https://jsfiddle.net/53p5b3w8/5/</a></p>