So i have been having some problems with javascript’s map method. For some reason i cannot access objects properties from map function. Even though i am using the function on an array. When i print out the object the output is;
this is the output of console.log(hospitalMarkers)
And this is the code i use
In the console log, if i print the marker, it shows me the whole list Which it should only be one of the elements. So if i try to access any properties of the objects from the marker, i get undefined.
Been trying to understand what’s wrong for hours now and i am very close to losing it. What is wrong here ?
Advertisement
Answer
If I have to make a guess your structure is this.
hospitalMarkers = [ [... markers Array 1], [... markers Array 2]] or hospitalMarkers = [ [{Ad:1},{Ad:2}], [{Ad:3},{Ad:4}]]
so it’s a nested array
what you might be needing is:
hospitalMarkers.flat().map(() => { console.log(marker.Ad) }); // flat it make it come one level up // hospitalMarkers = hospitalMarkers = [ {Ad:1},{Ad:2},{Ad:3},{Ad:4}]
this way you will have all the markers at the same level.