我有两个对象数组,我需要根据我拥有的属性进行过滤。
var port = [
{
name: 'Cali',
type:'Mine',
location = {
lat: '89.9939',
lon: '-79.9999'
}
},
{
name: 'Denver',
type:'Port',
location = {
lat: '67.9939',
lon: '-85.9999'
}
},
{
name: 'Seattle',
type:'Port',
location = {
lat: '167.9939',
lon: '-85.9999'
}
},
...........
]
还有另一个对象
var child = [
{
lat: '89.9939',
lon: '-79.9999'
},
{
lat: '67.9939',
lon: '-85.9999'
}
]
我正在使用过滤器
var result = port.filter( function(el){
return el.location.lat === child.lat
});
我如何循环我的第二个数组。在这种情况下,我的数据相当大。
答案 0 :(得分:1)
您可以使用Array#some
来确定child
数组中的任何对象是否与lat
数组中的任何对象具有相同的port
值。
var port = [{name:'Cali',type:'Mine',location:{lat:'89.9939',lon:'-79.9999'}},{name:'Denver',type:'Port',location:{lat:'67.9939',lon:'-85.9999'}},{name:'Seattle',type:'Port',location:{lat:'167.9939',lon:'-85.9999'}}],
child = [{lat:'89.9939',lon:'-79.9999'},{lat:'67.9939',lon:'-85.9999'}],
result = port.filter(el => child.some(v => v.lat == el.location.lat));
console.log(result);