1

Here is my array:

const locations = [
    {
      results: [
        {
          name: "Rome, Italy",
          price: 100,
          distance: 1299,
          match: 96,
          lat: 40,
          lng: 60,
        },
        {
          name: "Beijing, China",
          price: 200,
          distance: 3000,
          match: 93,
          lat: 100,
          lng: 100,
        },
        {
          name: "California, USA",
          price: 250,
          distance: 420,
          match: 75,
          lat: 200,
          lng: 200,
        },
      ],
    },
    {
      results: [
        {
          name: "Rome, Italy",
          price: 100,
          distance: 1299,
          match: 96,
          lat: 50,
          lng: 60,
        },
        {
          name: "Beijing, China",
          price: 200,
          distance: 3000,
          match: 93,
          lat: 100,
          lng: 100,
        },
        {
          name: "California, USA",
          price: 250,
          distance: 420,
          match: 75,
          lat: 200,
          lng: 200,
        },
      ],
    },
  ];

Now, I want to take the lat and lng coordinates and pass them as props this component:

 {locations.map((location, index) => {
              return <MapMarker locations={locations[index]} />;
            })}

And here is the actual components code:

<MapMarkerContainersContainer>
      {locations.map((location, index) => {
        return (
          <AnyReactComponent
            lat={location.result[index].lat}
            lng={location.result[index].lng}
            text="My Marker"
          />
        );
      })}
    </MapMarkerContainersContainer>

Any idea what I'm doing wrong?

1
  • What is happening/not happening that is different from what you expect? Please add that information to your question. Commented May 18, 2021 at 22:25

3 Answers 3

2

You are using the index of the locations array. So instead of using location.result[index] you could reduce all the locations.result 's inside the locations array and loop over the results:

<MapMarkerContainersContainer>
{
  locations
    .reduce((acc, cur) => [...acc, ...cur.results], [])
    .map((location) => {
      return (
        <AnyReactComponent
          lat={location.lat}
          lng={location.lng}
          text="My Marker"
        />
      );
    })
}
</MapMarkerContainersContainer>
Sign up to request clarification or add additional context in comments.

Comments

2

You have array inside array, so you can first flat them out.

  const marks = []
  locations.forEach(v => {
    v.results.forEach(i => { mark.push(i) })
  })

And then

      {marks.map(m => 
          <AnyReactComponent
            key={`${m.lat}${m.lng}`}
            lat={m.lat}
            lng={m.lng}
            text="My Marker"
          />
      })}

You need key, or you will get errors.

Comments

1

You need to flatten the array before calling map.

<MapMarkerContainersContainer>
    {locations.reduce((prev, cur) => [...prev, ...cur.results], []).map((location, index) => {
        return (
            <AnyReactComponent
                lat={location.lat}
                lng={location.lng}
                text="My Marker"
            />
        );
    })}
</MapMarkerContainersContainer>

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.