Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Grep将值用作id_Javascript_Jquery - Fatal编程技术网

Javascript 使用Grep将值用作id

Javascript 使用Grep将值用作id,javascript,jquery,Javascript,Jquery,我正在使用grep从数组中删除一个对象。如果键相同,它会工作,但是如果它与值myVar匹配,我希望它从数组中移除(我使用的是唯一的值),但我无法进入对象内部,因为我只能访问索引 如何从数组中删除myVaretc中包含的具有巴黎、伦敦任何值的对象 function myFunction(){ var arr = [ {country:"USA"}, {country:"France"}, {country:"England

我正在使用grep从数组中删除一个对象。如果键相同,它会工作,但是如果它与值
myVar
匹配,我希望它从数组中移除(我使用的是唯一的值),但我无法进入对象内部,因为我只能访问索引

如何从数组中删除
myVar
etc中包含的具有巴黎、伦敦任何值的对象

function myFunction(){
        var arr = [
          {country:"USA"},
          {country:"France"},
          {country:"England"},
          {city:"New York"},
          {city:"Paris"},
          {city:"London"},
        ];

        var myVar = 'Paris';//Change variable
        arr = $.grep(arr, function(data, index) {
           return data.GET_TO_MY_VALUE!= myVar;
        });
        console.log(arr);
    }
    myFunction();
函数

要具有多个条件,请使用
|
运算符

函数myFunction(myVar){
var arr=[{
国家:“美国”
}, {
国家:“法国”
}, {
国家:“英格兰”
}, {
城市:“纽约”
}, {
城市:“巴黎”
}, {
城市:“伦敦”
}];
return$.grep(arr,函数(数据,索引){
返回(data.city&&data.city!=myVar)| |(data.country&&data.country!=myVar);
});
}
log(JSON.stringify(myFunction('Paris'));
log(JSON.stringify(myFunction('England'))
函数

要具有多个条件,请使用
|
运算符

函数myFunction(myVar){
var arr=[{
国家:“美国”
}, {
国家:“法国”
}, {
国家:“英格兰”
}, {
城市:“纽约”
}, {
城市:“巴黎”
}, {
城市:“伦敦”
}];
return$.grep(arr,函数(数据,索引){
返回(data.city&&data.city!=myVar)| |(data.country&&data.country!=myVar);
});
}
log(JSON.stringify(myFunction('Paris'));
log(JSON.stringify(myFunction('England'))

预期输出是什么<代码>{城市:“巴黎”}
?或
[{“国家”:“美国”},{“国家”:“法国”},{“国家”:“英格兰”},{“城市”:“纽约”},{“城市”:“伦敦”}]
?删除包含ParisUse数组.filter的对象
arr=arr.filter(函数(数据,索引){return data.city!=myVar;})
对于contain,使用RegEx
var RegEx=new RegExp(myVar,'i');arr=arr.filter(函数(数据,索引){return regex.test(data.city);})我不需要data.city,但是如果我需要data.country呢。它需要在值上是动态的预期输出是什么<代码>{城市:“巴黎”}
?或
[{“国家”:“美国”},{“国家”:“法国”},{“国家”:“英格兰”},{“城市”:“纽约”},{“城市”:“伦敦”}]
?删除包含ParisUse数组.filter的对象
arr=arr.filter(函数(数据,索引){return data.city!=myVar;})
对于contain,使用RegEx
var RegEx=new RegExp(myVar,'i');arr=arr.filter(函数(数据,索引){return regex.test(data.city);})我不需要data.city,但是如果我需要data.country呢。它需要在值上是动态的