0

I want to be able to remove values from an array which contain the value round.

An example array submitted could be [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

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]

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);

1 Answer 1

1

Use the filter function and use the RegExp test method.

var arr = ["team1", "team2", "round3", "round5", "team3", "round6"]
arr = arr.filter(e => !/round/.test(e));
console.log(arr);

Sign up to request clarification or add additional context in comments.

2 Comments

Smashed it Richard! What is browser compatibility like for such a function?
It should be functional in most browsers if I remember correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.