简体   繁体   中英

remove object from object array

Hello I was trying to remove objects from object array that I have and then create another new object (I'm using $.map() to create the new object )

to remove this object from object(x) it's object.number has to match one of the number the number in array(y)

this following code works but i only remove the object that has the object.number = 40 DEMO

Code :

   var x =[ //this is the object 
  {name : 'mark' , number : '10' , color:'green'},
  {name : 'jeff' , number : '15' , color:'blue'} ,
  {name : 'joy' , number : '30' , color:'yellow'},
  {name : 'mick' , number : '15' , color:'red'},
  {name : 'mick' , number : '40' , color:'black'}] ; 

      var y =['40','15']; // i need to remove all object.number that match the 
        // number in this array

     var newObject = $.map(x  ,function(index, value){
        for(i in y){
         if(index.number == y[i])
        {return null ; }
       else{
        return index;
            }      
      }

    });
 console.log(newObject);​

the code above only remove the object that has 40 in it's object.number, how can i make this work?

This is what you want:

var newObject = $.map(x  ,function(index, value){
    for(i in y){
        if(index.number == y[i])
            return null;
    }
    return index;
});
console.log(newObject);​

and jsFiddle . By the way: I think using for(i in y) for arrays is not a good practice (arrays may have other properties). You should use standard:

var l = y.length;
for(var i = 0; i < l; i++){
    /* the other code */
}

Note the length caching.

Try this code:

var newObject = $.map(x, function(index, value){  
    return (y.indexOf(index.number) != -1) ? null : index;
});

http://jsfiddle.net/Dc68W/

Try changing your function like below,

var newObject = $.map(x, function(value, index) {   
   if ($.inArray(value.number, y) == -1) {
       return value;
   } else {
       return null;
   }
});

DEMO: http://jsfiddle.net/skram/C3d9T/7/

Use following:

var newObject = $.map($.makeArray(x), function(index, value){  
    return y.indexOf(index.number) != -1 ? null : index;
});

$.makeArray() is not required, but recommended, if your array can include disordered data.

I've made an update of your fiddle here . Basically you weren't checking each value in the delete array.

var x =[
{name : 'mark' , number : '10' , color:'green'},
{name : 'jeff' , number : '15' , color:'blue'} ,
{name : 'joy' , number : '30' , color:'yellow'},
{name : 'mick' , number : '15' , color:'red'},
{name : 'mick' , number : '40' , color:'black'}] ; 

var y =['40','15'];

var newObject = $.map(x  ,function(index, value){

    var valid = true;
    for(var i = 0; i < y.length; i++){

        if(index.number == y[i])
           valid = false;             
    }
    if(valid)return index;
    else return null;
    });
console.log(newObject);​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM