[英]Remove a specific value from array in each loop - jQuery
I want to be able to remove values from an array which contain the value round
. 我希望能够从包含值
round
的数组中删除值。
An example array submitted could be [team1, team2, round3, round5, team3, round6]
提交的示例数组可以是
[team1, team2, round3, round5, team3, round6]
I have this code which is meant to loop through the array and remove an element from the array if it contains round
using search
我有这段代码,旨在遍历数组,并使用
search
从数组中删除元素(如果包含round
function modifyFilterConfig(idStr, target) {
$.each(idStr, function(i, value){
console.log("The index of this value is: " + i);
console.log("The value is: " + value);
console.log("The value match is: " + value.search(target));
if(value.search(target) == 0) {
// IF THE ARRAY CONTAINS A ROUND REMOVED IT FROM THE ARRAY
// i - 1 is the current index of the array (i.e. the ROUND filter(s))
idStr = idStr.splice(i - 1, 1);
}
i = i++;
});
console.log("Return value:");
console.log(idStr);
return idStr;
}
At the moment the results I get are the last team in the array for example team6
but my desired result is [team2, team3, team6]
目前,我得到的结果是数组中的最后一个团队,例如
team6
但我想要的结果是[team2, team3, team6]
Finally, I want the function to return an array as well so I use it the array again in my wider code. 最后,我希望函数也返回一个数组,因此我将在更广泛的代码中再次使用该数组。
console.log("IDS before processing: " + ids); var ids = modifyFilterConfig(ids, "round"); console.log("IDS after processing: " + ids);
Use the filter
function and use the RegExp test
method. 使用
filter
功能并使用RegExp test
方法。
var arr = ["team1", "team2", "round3", "round5", "team3", "round6"] arr = arr.filter(e => !/round/.test(e)); console.log(arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.