0

I have an array with user who has a property with the latitude. How can i do if i want to create a new array with user who has the same latitude ?

my array looks like this :

let arrayToFilter = [
  {
  "user1" : {
    "profile" : {
      "name" : "user1",
      "age" : "35",
      "gender" : "male",
      "latitude" : 57.267801888216965,
      "longitude" : 16.451598081831214
    }
  },
  "user2" : {
    "profile" : {
      "name" : "user2",
      "age" : "50",
      "gender" : "male",
      "latitude" : 37.785834,
      "longitude" : -122.406417
    }
  },
  "user3" : {
    "profile" : {
      "name" : "user3",
      "age" : "23",
      "latitude" : 37.785834,
      "longitude" : -122.406417
    }
  }
}
]

i´ve tried with this, but this does not seem to work...

let arr = arrayToFilter.filter(child => child.latitude === child.latitude)
charles
  • 73
  • 3
  • 12
  • Please share the objects as well. Apart from a missing `)` your code looks fine. – 31piy May 08 '18 at 14:51
  • 1
    There's no missing `)`, and, despite looking fine, the code wouldn't do much, as it doesn't really filter the array. – bugs May 08 '18 at 14:55
  • Same latitude as what? You're referring to the same `child` twice in your code. – Bergi May 08 '18 at 14:56

2 Answers2

0

The error with your code is that you are comparing the user to itself. Your filter queries whether the user's latitude is equal to its own latitude, this is true for all users and you will get an array back with all users.

It should instead be:

let arr = arrayToFilter.filter(child => child.latitude === latitudeToCompare)

Where latitudeToCompare is the latitude at which you would like to find users with the same latitude.

If what you are looking for is a "group by" option in Javascript, then as per this Stackoverflow post can be implemented with a slight variation as follows:

var groupBy = function(xs, [min, max]) {
  return xs.reduce(function(rv, x) {
    (min < rv[x['latitude']] < max || []).push(x);
    return rv;
  }, {});
};

where xs is the array of user objects, and min/max are the minimum and maximum latitude to create a range or bucket of latitudes.

You should be able to use the above to help you figure out a solution for your problem.

Benjamin Scholtz
  • 823
  • 6
  • 15
  • okey thank you, that make sense ! how would be a smart way to implement "latitudeToCompare" ? – charles May 08 '18 at 15:04
  • Judging by what you posted, I don't really think this is what you are trying to do. My guess is that you'd like to build an array of all the elements with the same latitude. Is this correct? of course, you would need to generate more than one array, as there could be multiple subgrups – bugs May 08 '18 at 15:06
  • I've extended my answer if the above is your intended use case. – Benjamin Scholtz May 08 '18 at 15:33
0

the problem is your arrayToFilter, it is an array but it has just one item and inside that object you have a key/value user with the information, thus the steps are the following:

  1. loop over the array.
  2. loop over the keys to get the items
  3. finally loop over the users to get the desired info.

notice that because we are doing a map then another map and then filter we are going to get a similar structure as you gave. Array -> Array -> UserObject.

let user = {
  name: 'test',
  latitude: 57.267801888216965
}

let arrayToFilter = [{
  "user1": {
    "profile": {
      "name": "user1",
      "age": "35",
      "gender": "male",
      "latitude": 57.267801888216965,
      "longitude": 16.451598081831214
    }
  },
  "user2": {
    "profile": {
      "name": "user2",
      "age": "50",
      "gender": "male",
      "latitude": 37.785834,
      "longitude": -122.406417
    }
  },
  "user3": {
    "profile": {
      "name": "user3",
      "age": "23",
      "latitude": 37.785834,
      "longitude": -122.406417
    }
  }
}]


//we have to loop over the array because it is an array of objects and the users are actually inside the object of the first item.
let filtered = arrayToFilter.map(items => {
  //here we are going to loop to get the keys and then iti will return us an arrar
  //with the items inside, then we can apply the filter.
  return Object.keys(items).map(k => items[k]).filter(u => u.profile.latitude === user.latitude)
})

console.log(filtered);
Prince Hernandez
  • 3,623
  • 1
  • 10
  • 19