Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an object with string values attached to the same property that I wish to remove using a function if that value is present. To achieve this I have placed the string values into an array, iterated through them and want to remove the value using splice() . The problem is that splice() doesn't seem to recognize the array counter. It always removes the first item in the array even when the counter is is higher. I've checked the code and the loop is working correctly.

I've not seen anything online to suggest that splice can't be used with a variable at the index. How can I make splice use the counter to remove the relative value ?

var obj = {

className: 'open menu next thing'
}

function removeClass(elem, cls) {

var arr = elem.className.split(' ');

for(var i = 0; i < arr.length; i++) {
    if(arr[i] == cls) {
        alert(arr[i]);
        var splCout = arr.splice(arr[i], 1);
        alert(splCout);
    }
}
var str = arr.join(' ');
alert(str);
}

removeClass(obj, 'next');
share|improve this question
2  
First argument of splice is not a value, it's a start index. You should write somethnig like arr.splice(i, 1); –  u_mulder Jan 4 '14 at 14:07
    
Ah i see my error, thanks –  moonshineOutlaw Jan 4 '14 at 14:11
    
check my answer out and fix your code, considering those 2 points. –  Mehran Hatami Jan 4 '14 at 14:27

1 Answer 1

up vote 0 down vote accepted

You have 2 different problem to fix:

1- you are modifying the array object in your for loop, after the first splice the array is changed and the arr.length is totally different for the rest of your loop items, to prevent it create a variable like len and set it to the length of your array once.

but if you change the loop the other way round, you would need it once:

for(var i = arr.length; i >= 0; i--) {
    if(arr[i] == cls) {
        alert(arr[i - 1]);
        var splCout = arr.splice(arr[i - 1], 1);
        alert(splCout);
    }
}

2- the next and the more important problem is the indexes of the array, when you remove an index the other indexes change (i......n) and the code won't work properly, but as you see when you iterate the array this way even when you remove the index, the other indexes back to the zero (i....0) don't change.

share|improve this answer
    
Is their a reason why I couldn't drop the variable and write the loop like this for(var i = arr.length; i >= 0; i--) ? –  moonshineOutlaw Jan 4 '14 at 15:03
    
no it is just to make sure the for loop would iterate the all indexes if you start with 0, but when you do it my way, it doesn't matter any more. I think i'd better change my answer. –  Mehran Hatami Jan 4 '14 at 15:23
    
Great help, thanks –  moonshineOutlaw Jan 5 '14 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.