Looping through array error
After the first iteration of the loop, the array is already loaded with all the objects. How do I load only the first object on the first iteration?
by Ben Grunfeld
HTML
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
JavaScript
// Here is a reproduction of the error
var obj = [
{
"id": 1111,
"todoText": {"index":2,"items":[{"firstName":"Benny"},{"lastName":"Hill"}]}
},
{
"id": 2222,
"todoText": {"index":2,"items":[{"firstName":"Bob"},{"lastName":"Marley"}]}
},
{
"id": 3333,
"todoText": {"index":2,"items":[{"firstName":"John"},{"lastName":"Lennon"}]}
},
{
"id": 4444,
"todoText": {"index":2,"items":[{"firstName":"Led"},{"lastName":"Zeplin"}]}
},
];
var myArr = [];
$.each(obj, function(key, item) {
console.log(myArr);
console.log(item);
myArr.push(item);
});