Skip to content
Advertisement

Javascrip cannot access object properties from map method

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;

Output of console.log(marker)

this is the output of console.log(hospitalMarkers)

And this is the code i use

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement