我希望能够从包含值round
的数组中删除值。
提交的示例数组可以是[team1, team2, round3, round5, team3, round6]
我有这个代码,它用于遍历数组并使用round
search
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;
}
目前我获得的结果是数组中的最后一个团队,例如team6
,但我想要的结果是[team2, team3, team6]
最后,我希望函数返回一个数组,所以我在更广泛的代码中再次使用它。
console.log("IDS before processing: " + ids);
var ids = modifyFilterConfig(ids, "round");
console.log("IDS after processing: " + ids);
答案 0 :(得分:1)
使用test
功能并使用RegExp var arr = ["team1", "team2", "round3", "round5", "team3", "round6"]
arr = arr.filter(e => !/round/.test(e));
console.log(arr);
方法。
{{1}}