I have the following method:
var items = [1,2,3];
$.map(items, function (item) {
if (item === 1) {
items.push(4);
}
console.log(item);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
and I expect in console 1,2,3,4, but see 1,2,3. I mean I want to see one extra loop item.
Can I resolve it somehow? And if yes, how can I resolve it?
itemuse thisconsole.log(items);this will give expect output..map()stores the original length of the array before invoking the callback (step 2). By design, it won't continue into new elements added during iteration.console.log(item);needs to beconsole.log(items);. Seems TYPO mistake to me.map()for this?