简体   繁体   English

根据另一个数组数组过滤对象数组

[英]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 : 您可以使用Array.filterArray.someArray.every做到这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. 这个想法是先过滤主数组及其内部的过滤器,以确保至少有一个filters所有元素在LatLng数组中都匹配。

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: 而不是filter ,您可以从nearest_array .map ,然后.find关联的LatLng项-如果输入数组和输出数组是一对一的, .map会更合适:

 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) (非原始对象,例如对象,数组和函数,彼此不相等,除非它们引用内存中的相同项目-例如[1, 2] !== [1, 2]因为那里有两个单独的数组 ,每个文件在内存中的位置都不同。因此,您的.includes测试无法正常进行)

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. 您可以使用该功能filter并检查latitudelongitude使用功能some ,这是用于检查包含这些坐标至少一个对象。

 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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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