从每个循环中的数组中删除一个特定的值 - jQuery

时间:2017-01-31 16:53:52

标签: javascript jquery arrays

我希望能够从包含值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);

1 个答案:

答案 0 :(得分:1)

使用test功能并使用RegExp var arr = ["team1", "team2", "round3", "round5", "team3", "round6"] arr = arr.filter(e => !/round/.test(e)); console.log(arr);方法。



{{1}}