Thursday, July 3, 2014

Javascript Function To Remove Array element by Value

function removeByValue(arr, val) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}
 
var somearray = ["mon", "tue", "wed", "thur"]
 
removeByValue(somearray, "tue");
 
//somearray will now have "mon", "wed", "thur"

No comments:

Post a Comment