Bienvenido Omosura
Bienvenido Omosura

Reputation: 127

Removing a specific value in Array

for example, I have this an array of object:

var myarray = [{ id: 01, name: alison, active: false},
               { id: 04, name: drex, active: true},
               { id: 08, name: farel, active: true},
               { id: 09, name: dinkoy, active: true}];

I want to delete some of the objects where active == false, or id == 09.

any code suggestion?

Upvotes: 2

Views: 63

Answers (6)

RobG
RobG

Reputation: 147343

You asked for the original array to be modified, answers using filter create a new array. Other methods to use are forEach, reduce and reduceRight.

Since forEach remembers the original array, you don't need to do anything special when removing members:

myarray.forEach(function(v, i, arr) {
  if (v.active == false || v.id == 09) arr.splice(i, 1);
});

reduceRight goes from length to 0 so more appropriate when modifying the array, so:

myarray.reduceRight(function(p, c, i, arr) {
   if (c.active == false || c.id == 09) arr.splice(i, 1);
},null);     

But reduce is just like forEach in that it keeps track of indexes if members are removed, so:

myarray.reduce(function(p, c, i, arr) {
   if (c.active == false || c.id == 09) arr.splice(i, 1);
},null);     

works too.

Upvotes: 0

Anish Nair
Anish Nair

Reputation: 3368

You can perform a foreach loop and check this condition active == false, or id == 09

Here's the snippet

Method 1 - JSFiddle Link

//Your array..
var myarray = [{ id: 01, name: 'alison', active: false},
               { id: 04, name: 'drex', active: true},
               { id: 08, name: 'farel', active: true},
               { id: 09, name: 'dinkoy', active: true}];

//Perform a for-each loop on the array..
for(var key in myarray) {
    //if object->id = 9 OR object->active = false, remove the object from the array

    if( (myarray[key].id == 9) || (myarray[key].active == false) ) {
        delete myarray[key];
    }
}

Note: This method does not affect the length of the array - even after deleting the items, the length remains 4

Method 2 - JSFiddle Link

//Your array..
var myarray = [{ id: 01, name: 'alison', active: false},
               { id: 04, name: 'drex', active: true},
               { id: 08, name: 'farel', active: true},
               { id: 09, name: 'dinkoy', active: true}];

for(var key in myarray) {
    //if object->id = 9 OR object->active = false, remove the object from the array
    if( (myarray[key].id == 9) || (myarray[key].active == false) ) {
        myarray.splice(key, 1);
    }
}

This works fine, the length of the array is affected as well - 2

Hope it helps.

Upvotes: 0

sinhayash
sinhayash

Reputation: 2803

Use filter. More here

myarray = myarray.filter(function(e){
    return (e.active !== false) && (e.id !== 09);
});

I recommend using underscore.js or sugar.js for common tasks like this:

underscore.js

myarray = _.reject(myarray, function(e){
    return (e.active !== false) && (e.id !== 09);
});

sugar.js

myarray.remove(function(e){
    return (e.active !== false) && (e.id !== 09);
});

Raw JS loop:

for (i = myarray.length-1; i >= 0; i--)  {
    if (myarray[i].active === false || myarray[i].id === 09) myarray.splice(i, 1);
}

Upvotes: 5

vp_arth
vp_arth

Reputation: 14982

Something like this:

var i, item;
for (i = items.length-1; i >= 0; i--) {
  item = items[i];
  if (item.active===false || item.id = 9) {
    items.splice(i, 1);
  }
}

As you can see, countdown loop used, it because we should modify iterated array carefully :)

Upvotes: 2

Rajeev
Rajeev

Reputation: 41

If you are using JQuery then you can use 'each' function for looping and then splice the object which active == false.

Upvotes: 1

galactocalypse
galactocalypse

Reputation: 1935

You could use the filter function for this:

myarray = myarray.filter(function(e){
    return (e.active !== false) && (e.id !== 09);
});

Or use a simple while loop, like this:

var i = 0;
while(i < myarray.length) {
    if (myarray[i].active === false || myarray[i].id === 09) myarray.splice(i, 1);
    else i++;
}

Maintaining indices can get tricky in this kind of an implementation, so choose whatever you find more convenient.

Upvotes: 2

Related Questions