Possible Duplicate:
Loop through array in JavaScript
I want to make the equivalent of php's foreach in javascript. Because I don't really know the Javascript language, I'd like someone to rewrite this PHP code into the Javascript piece:
$my_array = array(2 => 'Mike', 4 => 'Peter', 7 => 'Sam', 10 => 'Michael');
foreach($my_array as $id => $name)
{
echo $id . ' = ' . $name;
}
Is that even possible to do in the Javascript language?
for (var item in myObjectHash)
orfor (var i = 0; i < myArray.length; i++)
If you're working with actual arrays, don't usefor...in
.forEach
otherwise you would have to do afor
loop.for...of
is the equivalent, notfor...in
: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…