Looping through arrays is easy:
var images = [
{
src: '/zero.png',
title: 'Zero'
},
{
src: '/one.png',
title: 'One'
},
{
src: '/two.png',
title: 'Two'
}
];
images.forEach(function(element, index, array) {
console.log(index, element);
});
But what if you have an object like this which really looks like it should have been an array?
var images = {
'0': {
src: '/zero.png',
title: 'Zero'
},
'1': {
src: '/one.png',
title: 'One'
},
'2': {
src: '/two.png',
title: 'Two'
}
};
Easy too, just different:
for (var key in images) {
// skip loop if the property is from prototype
if (images.hasOwnProperty(key) === false) continue;
console.log(key, images[key]);
}