I am trying to loop through a JS object parsed from JSON code in [this.props.dataPro.links]:
"links" :
[ "/static/media/0.jpg",
"/static/media/1.jpg",
"/static/media/1.jpg" ],
I am inserting these links by array key into an images object:
_getStaticImages() {
let images = [this.props.dataPro.links]
for (var key in images) {
if (images.hasOwnProperty(key)) {
var obj = images[key];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
images.push({
original: obj[prop],
thumbnail:obj[prop]
});
}
}
}
}
return images;
}
As a result I get an empty returned object at the beginning of my return object, when I do a console.log on the images response it seems I also return the array object itself:
[Array[3], Object, Object, Object]
0:Array[4]
1:Object
2:Object
3:Object
How can I sort loop through my array and exclude the array itself?
for .. inloop. Why not just iterate by dereferencing the arraylinks[i]or use Array.forEach()?for .. inbecauselinksis a propertylinksandlinksis defined:"links": [...]. Solinksitself is an array and can be iterated as such.