Javascript 比较数组并删除不匹配的值

Javascript 比较数组并删除不匹配的值,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我有两个数组。如果位置不包含模板定位中的某些名称,我想将它们从模板定位中删除。在这种情况下,我想删除位置全局 我尝试了以下方法,但不起作用 $scope.locations = [ { name : "One"}, { name : "Two"}, { name : "Three"}, { name : "India"}, { name : "Japan"}, { name : "China"} ]; $scope.tempLocations =

我有两个数组。如果
位置
不包含
模板定位
中的某些名称,我想将它们从
模板定位
中删除。在这种情况下,我想删除位置全局

我尝试了以下方法,但不起作用

$scope.locations = [
    { name : "One"},
    { name : "Two"},
    { name : "Three"},
    { name : "India"},
    { name : "Japan"},
    { name : "China"}
];

$scope.tempLocations = [
    { name : "One"},
    { name : "Two"},
    { name : "global"},
];

for(var i=0;i我想您正在寻找这个

$scope={}
$scope.locations=[
{name:“一”},
{name:“两个”},
{名称:“三”},
{名称:“印度”},
{名称:“日本”},
{名称:“中国”}
];
$scope.templations=[
{name:“一”},
{name:“两个”},
{name:“global”},
];
$scope.tempLocations=$scope.tempLocations.filter(函数(x){
返回$scope.locations.some(函数(y){
返回x.name==y.name
})
})
document.getElementById(“输出”).innerHTML=JSON.stringify($scope.templations,0,”);
console.log($scope);

我想你在找这个

$scope={}
$scope.locations=[
{name:“一”},
{name:“两个”},
{名称:“三”},
{名称:“印度”},
{名称:“日本”},
{名称:“中国”}
];
$scope.templations=[
{name:“一”},
{name:“两个”},
{name:“global”},
];
$scope.tempLocations=$scope.tempLocations.filter(函数(x){
返回$scope.locations.some(函数(y){
返回x.name==y.name
})
})
document.getElementById(“输出”).innerHTML=JSON.stringify($scope.templations,0,”);
console.log($scope);

您需要手动循环,正如Paul S.的评论所建议的:
var位置=[
{name:“一”},
{name:“两个”},
{名称:“三”},
{名称:“印度”},
{名称:“日本”},
{名称:“中国”}];
变量模板配置=[
{name:“一”},
{name:“两个”},
{name:“global”},
];
var newTempLocations=模板位置.filter(函数(临时){
返回位置。某些(函数(位置){//在第一次匹配时停止并返回true
返回location.name==临时名称;
});
})
//打印输出
document.getElementById(“输出”).innerHTML=JSON.stringify(newTempLocations,null,”);

您需要手动循环,正如Paul S.的评论所建议的:
var位置=[
{name:“一”},
{name:“两个”},
{名称:“三”},
{名称:“印度”},
{名称:“日本”},
{名称:“中国”}];
变量模板配置=[
{name:“一”},
{name:“两个”},
{name:“global”},
];
var newTempLocations=模板位置.filter(函数(临时){
返回位置。某些(函数(位置){//在第一次匹配时停止并返回true
返回location.name==临时名称;
});
})
//打印输出
document.getElementById(“输出”).innerHTML=JSON.stringify(newTempLocations,null,”);

如果
$scope.locations
不会经常更改,您可以执行以下操作:

构建位置的查找表

for(var i=0;i<$scope.tempLocations.length;i++){
    var index = $scope.tempLocations.indexOf($scope.locations[i]);
    if(index == -1){
        console.log($scope.tempLocations[i]);
        $scope.tempLocations.splice(i,1);
    }
}
如果筛选的频率高于重新计算查找所需的频率,则这将被证明快得多。因此,如果
$scope.locations
是静态的,这将是一个很好的方法

我建议不要像另一张海报所说的那样在location\u lookup
中使用
temp.name,因为这也会检查
location\u lookup
对象的所有原型属性。例如,如果应用程序中的另一个脚本执行了
object.prototype.global=function(){}
然后过滤器将返回“global”作为
$scope.locations的一部分,这不是您想要的行为。
hasOwnProperty
将只检查对象本身,而不是任何原型继承,同时也是一种更有效的方法


Fiddle演示:(还包括一个使用Array.prototype添加
.filter_locations()
方法的实现,但添加到Array.prototype通常是个坏主意)

如果
$scope.locations
不会经常更改,您可以执行以下操作:

构建位置的查找表

for(var i=0;i<$scope.tempLocations.length;i++){
    var index = $scope.tempLocations.indexOf($scope.locations[i]);
    if(index == -1){
        console.log($scope.tempLocations[i]);
        $scope.tempLocations.splice(i,1);
    }
}
如果筛选的频率高于重新计算查找所需的频率,则这将被证明快得多。因此,如果
$scope.locations
是静态的,这将是一个很好的方法

我建议不要像另一张海报所说的那样在location\u lookup
中使用
temp.name,因为这也会检查
location\u lookup
对象的所有原型属性。例如,如果应用程序中的另一个脚本执行了
object.prototype.global=function(){}
然后过滤器将返回“global”作为
$scope.locations的一部分,这不是您想要的行为。
hasOwnProperty
将只检查对象本身,而不是任何原型继承,同时也是一种更有效的方法


Fiddle演示:(还包括一个使用Array.prototype添加
.filter_locations()
方法的实现,但添加到Array.prototype通常是个坏主意)

对象比较是ByRef,所以
{}==={};//false
因此您不能执行
索引;您需要循环手动对象比较是按REF进行的,所以
{}=={};//false
所以不能执行索引;需要循环manually@xec您不希望在validLocations中使用
x.name,因为这也会检查添加到原型中的任何内容。例如,
Object.prototype.global=…
会导致“全局”在
模板定位中
进行匹配,尽管不在
位置
。是否要使用
hasOwnProperty()
因为这只会检查数据结构本身,也会带来性能的小幅度提升。@xec您不希望在validLocations中使用
x.name
,因为这也会检查添加到原型中的任何内容。例如,
Object.prototype.global=…
会导致“全局”在
模板定位中
进行匹配,尽管不是i
$scope.filteredLocations = $scope.tempLocations.filter(function(temp) {
    return location_lookup.hasOwnProperty(temp.name);
});