0

Data :

 const zones =  [
       {
          "ID":"4131",
          "RID":"f1438b",
          "Name":"Hero"
       },
       {
          "ID":"6266",
          "RID":"cd074d",
          "Name":"Villan"
       }
    ]

My Code :

<Polygon positions={}>
        <Popup>
         {poly.ID}
        </Popup>
</Polygon>

Where, poly.ID is "4131".

I need to display Name instead of poly.ID. And Name is obtained from the above Data, where poly.ID is mapped to get the Name.

I tried,

<Polygon positions={}>
        <Popup>
         {zones.map((zone) => poly.ID === zone.Name)}
        </Popup>
</Polygon>

But it, doesn't seem to render Name nor nothing

4 Answers 4

2

If you just want to find one element and display its name, here is how you can do :

zones.find(x => x.ID === poly.ID).Name

Note I am using find, not map.

Sign up to request clarification or add additional context in comments.

Comments

1

You have wrong condition in your map function, you are checking ID against name which will always be false, try this:

<Polygon positions={}>
        <Popup>
         {zones.map((zone) => poly.ID === zone.ID)}
        </Popup>
</Polygon>

Comments

1

You can use filter:

<Polygon positions={}>
        <Popup>
         {zones.filter((zone) => poly.ID === zone.ID)[0].Name}
        </Popup>
</Polygon>

Comments

1

zones.find(x => x.ID === poly.ID) might return undefined if no match is found. You can add a check like this:

const zone = zones.find(x => x.ID === poly.ID);

<Polygon positions={}>
        <Popup>
         {zone && zone.Name}
        </Popup>
</Polygon>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.