I am trying to arrange an array of objects in this context of a house, and I am trying to write a function in which users can arrange an array of houses by reference distance. Let's say I have the following data returned by the API that I call through AJAX:
"data" : [ { 'id' : 123, 'address' : '12 street name', 'city' : 'City Name', 'landmarks' : [ { 'name' : 'landmark one', 'distanceInMiles' : 0.6 }, { 'name' : 'landmark two', 'distanceInMiles' : 0.4 } ] }, { 'id' : 345, 'address' : '22 street name', 'city' : 'City Name', 'landmarks' : [ { 'name' : 'landmark one', 'distanceInMiles' : 0.2 }, { 'name' : 'landmark three', 'distanceInMiles' : 0.1 } ] }, { 'id' : 456, 'address' : '28 street name', 'city' : 'City Name', 'landmarks' : [ { 'name' : 'landmark six', 'distanceInMiles' : 8.2 }, { 'name' : 'landmark seven', 'distanceInMiles' : 1.6 } ] } ]
I already have a code that will return houses containing a specific landmark, for example. filter this array to return houses that have a landmark one landmark, and I store the filtered data in a separate array. But now I want to take another step and order a sorted array of results based on the distanceInMiles value for the selected landmark.
So, adhering to this context, I'm trying to write a code that returns two houses with the address "22 street name", and the house with the address "12 street name" second, since "22 street name", the house is closer to the landmark than the other.
I use _loadash as my utility library, but try to figure out how to sort the filtered array by reference distance. Any ideas?
Any help would be greatly appreciated.