0

I have a javascript function which delete object from object array:

function RemoveFilter(filtArray,filtName) {
    var filtCount = filtArray.length;

    ...
    ...

    for (var i = 0; i < filtCount; i++) {
        var filter = filtArray[i];
        if (filter != undefined && filter.name == filtName) {
            delete filtArray[i];
        }
    }

    ...
    ...
}

It's works but I got a big problem. Instead of removing object completelly, it leaves undefined on its place (that why I have filter != undefined in my if).

So basically, if I am adding something after removal, I have not only new values but also those undefiled, and array is growing.

Probably my choise of object removing is poor but, how can I deal with this problem? Considering that filtArray[i].remove not working at all.

3
  • 1
    You have missed the () on remove() function Commented Mar 23, 2015 at 11:45
  • possible duplicate Commented Mar 23, 2015 at 11:46
  • For such a task an elegant solution, if you're using jQuery, would be using the .filter prototype as an alternative to the splice method. Is your array an associative array? Check georg's solution below! Commented Mar 23, 2015 at 11:49

3 Answers 3

3

Instead of trying to modify an array in place, which is messy and error prone, just create a new array using filter and assign it back to the original:

myArray = myArray.filter(function(item) {
    return item.name !== name
});
Sign up to request clarification or add additional context in comments.

2 Comments

Oh darn, just said that above in the comments. This is a much cleaner solution instead of a .splice and it also is way more versatile, due to the fact that you might slightly edit that filter in order to delete more items providing a regular object. Bump that solution! +1
Thank you! This was the best one. No questionable splice and also allows to throw to oblivion "for".
2

You want to use splice() instead of delete. That should work.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");

will give:

Banana,Orange,Lemon,Kiwi,Apple,Mango

If you don't want to add new elements while removing others, you might wanna do this;

fruits.splice(2, 1);

The result of fruits will be:

Banana,Orange,Lemon

Shamelessly copied from W3Schools

In your case,

function RemoveFilter(filtArray,filtName) {
    var filtCount = filtArray.length;
    for (var i = 0; i < filtCount; i++) {
        var filter = filtArray[i];
        if (filter != undefined && filter.name == filtName) {
            filtArray.splice(i,1);
        }
    }
}

This will remove the i-th object and not leave an undefined hanging there.

4 Comments

An example that actually uses splice to remove items rather than insert new ones might be more useful.
Em and how it supposed to help? As I see in your example, it add new values and not removes them (also - how to do this in array?). Also, doit it update original array or what?
@OlegsJasjko, check now. I've added your scenario
@Olegs Corrected a mistake with the splice statement. Check now.
0

Using splice() is generally the way to go, and you can make it neat by doing something like this:

function removeFilter(filtArray, filtName) {
  var index = filtArray.indexOf(filtName);
  filtArray.splice(index, 1);
}

You can read more about splice() here.

EDIT:

To show you an example:

var dog = {name: 'Spot', type: 'Dog'}; 
var cat = {name: 'Meow', type: 'Cat'}; 
var animals = [dog, cat];

removeFilter(animals, dog); // -> Removes the dog object from the array

console.log(animals) // -> Only has the cat object remaining

Comments

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.