Home > Enterprise >  React combine two queries
React combine two queries

Time:03-17

What I want is to mixed the geoCode of location and people, how can I achieve that?

const calculate = (location, people) => {
   let geoCode = {}
    const peoples = people.map((people) = {
         return {
             geoCode: {
               latitude: a.geoCode.latitude,
               longitude: a.geoCode.longitude,
             }
         }
         return { //this is for location
             geoCode: {
               latitude: location[0].address.geoCode.latitude,
               longitude: location[0].address.geoCode.longitude,
             }
         }

     console.log(peoples)
   }
}

CodePudding user response:

You cannot return two objects from a map. You should merge the properties of location and people into a single object and then return that object. From your code I am assuming that both people and location have geocode.

const people = [{
    name: "People1",
    latitude: 111,
    longitude: 111,
  },
  {
    name: "People2",
    latitude: 222,
    longitude: 222,
  },
  {
    name: "People3",
    latitude: 333,
    longitude: 333,
  },
];

const loc = [{
  address: {
    geoCode: {
      latitude: 123,
      longitude: 345,
    }
  }
}];

const calculate = (location, people) => {
  let geoCode = {}
  const peoples = people.map((item) => {
    return {
      ...item,
      geoCode: {
        latitude: location[0].address.geoCode.latitude,
        longitude: location[0].address.geoCode.longitude,
      }
    };
  });

  console.log(peoples);
}

const res = calculate(loc, people);

  • Related