Home > Software design >  How can I get multiple keys of an object that are inside an array
How can I get multiple keys of an object that are inside an array

Time:07-29

I've been trying to find something similar to what I need to do but I haven't been able to find it quite yet.

I have an array of objects like so :

const places = [
  {
    City: 'Tribuckan',
    Lat: 48569.52,
    Long: 48.5
  },
  {
    City: 'Mecca',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Dazenda',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Uzumakin',
    Lat: 48569.52,
    Long: 48.5
  },
];

I want to take the Lat and Long from each object and return them so they can be used in a separate function that will figure out the distance from the original object to separate coordinates a client enters.

My theory is that I need to separate each object from the array and send it to a function that will store the coordinates and compare them to the clients coordinates.

This is what I tried but its not working so I am thinking I am way off base.

const places = [
  {
    City: 'Tribuckan',
    Lat: 48569.52,
    Long: 48.5
  },
  {
    City: 'Mecca',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Dazenda',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Uzumakin',
    Lat: 48569.52,
    Long: 48.5
  },
];

const firstFun = (places) => {
places.forEach(obj => funTest(obj));
}

const funTest = (obj) => {
  let Lat = obj.Lat;
  let Lon = obj.Long;
 
  compare(lat, lon);
}

const compare = (lat, lon, GUESTLAT, GUESTLON) => {
  let newLat = GUESTLAT - lat;
  let newLong = GUESTLON = lon
}

CodePudding user response:

Using the Array.prototype.map function should get you the results you're looking for. Below is an example.

const places = [
  {
    City: 'Tribuckan',
    Lat: 48569.52,
    Long: 48.5
  },
  {
    City: 'Mecca',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Dazenda',
    Lat: 48569.52,
    Long: 48.5
  },
    {
    City: 'Uzumakin',
    Lat: 48569.52,
    Long: 48.5
  },
];

const compareLatLong = (locationObject, guestLat, guestLong) => {
  return {
    Lat: locationObject.Lat - guestLat,
    Long: locationObject.Long - guestLong
  }
}

const guestLatitude = 1;
const guestLongitude = 1;

const getLatLongCompared = places.map(locationObject => {
  return compareLatLong({
    Lat: locationObject.Lat,
    Long: locationObject.Long
  }, guestLatitude, guestLongitude);
});

console.log(getLatLongCompared);

CodePudding user response:

As per my understanding, You are having a guest location object with their lat and long and you want to filtered out the object as per that location from the places array and update the lat and long. If Yes, Here you go :

// Input places array
const places = [
  {
    City: 'Tribuckan',
    Lat: 48569.52,
    Long: 48.5
  }, {
    City: 'Mecca',
    Lat: 48569.52,
    Long: 48.5
  }, {
    City: 'Dazenda',
    Lat: 48569.52,
    Long: 48.5
  }, {
    City: 'Uzumakin',
    Lat: 48569.52,
    Long: 48.5
  }
];

// Guest location object with different lat and long
const guestLocationObj = {
    City: 'Mecca',
  Lat: 55000.80,
  Long: 62.7
};

// Filtered out the object from places array based on the user location.
const filteredObject = places.find(({ City }) => City === guestLocationObj.City);

// Calculate the lat and long
const guestLat = guestLocationObj.Lat - filteredObject.Lat;
const guestLong = guestLocationObj.Long - filteredObject.Long;

// final output
console.log(guestLat, guestLong);

  • Related