简体   繁体   中英

Filter array of objects based on another array of arrays

I have an array of objects with latitude and longitude stored like this and filter array of arrays containing latitudes and longitudes. I want to filter my data based on latitudes and longitudes.

var data = [{LatLng:[34.09755005, -118.2900766],all_time: 22, wait_list: 217}
{LatLng:[21.30799045, -157.853676],all_time: 23, wait_list: 210},
{LatLng:[36.0678305, -110.2900766],all_time: 19, wait_list: 237},
{LatLng:[26.0665546, -130.8946739],all_time: 15, wait_list: 307}
]

I have another array of arrays which I am using as filters

var nearest_array = [[21.30799045, -157.853676],[26.0665546, -130.8946739]]

The result array should be this--

var result_array = [{LatLng:[26.0665546, -130.8946739],all_time: 15, wait_list: 307},{LatLng:[21.30799045, -157.853676],all_time: 23, wait_list: 210}]

I tried something but nothing seems to work--

data.filter(x => x.LatLng.some(g => nearest_array.includes(g)))

data.filter(x => nearest_array.includes(x.LatLng))

You can do this with Array.filter , Array.some and Array.every :

 const data = [{ LatLng: [34.09755005, -118.2900766], all_time: 22, wait_list: 217 }, { LatLng: [21.30799045, -157.853676], all_time: 23, wait_list: 210 }, { LatLng: [36.0678305, -110.2900766], all_time: 19, wait_list: 237 }, { LatLng: [26.0665546, -130.8946739], all_time: 15, wait_list: 307 } ] const filters = [ [21.30799045, -157.853676], [26.0665546, -130.8946739] ] const result = data.filter(({LatLng}) => filters.some(f => LatLng.every(l => f.includes(l)))) console.log(result) 

The idea is to first filter the main array and inside of that filter to make sure that at least one of the filters has all of its elements matched in the LatLng array.

Rather than filter , you might .map from your nearest_array , and .find the associated LatLng item - .map is a bit more appropriate if your input array and output array are one-to-one:

 var data=[{LatLng:[34.09755005,-118.2900766],all_time:22,wait_list:217},{LatLng:[21.30799045,-157.853676],all_time:23,wait_list:210},{LatLng:[36.0678305,-110.2900766],all_time:19,wait_list:237},{LatLng:[26.0665546,-130.8946739],all_time:15,wait_list:307}];var nearest_array=[[21.30799045,-157.853676],[26.0665546,-130.8946739]] const output = nearest_array.map((numsToFind) => { const joined = numsToFind.join(','); return data.find(({ LatLng }) => LatLng.join(',') === joined); }); console.log(output); 

(Non-primitives, like objects, arrays, and functions, are not equal to each other unless they reference the same item in memory - eg [1, 2] !== [1, 2] because there are two separate arrays there , each with a different place in memory. As a result, your .includes test won't work)

You can use the function filter and check for the latitude and longitude using the function some , this is for checking at least one object containing those coordinates.

 var data = [{LatLng:[34.09755005, -118.2900766],all_time: 22, wait_list: 217},{LatLng:[21.30799045, -157.853676],all_time: 23, wait_list: 210},{LatLng:[36.0678305, -110.2900766],all_time: 19, wait_list: 237},{LatLng:[26.0665546, -130.8946739],all_time: 15, wait_list: 307}], nearest_array = [[21.30799045, -157.853676],[26.0665546, -130.8946739]], result = data.filter(({LatLng: [lat, lon]}) => nearest_array.some(([ilat, ilon]) => lat === ilat && lon === ilon)); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM