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:
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:Note the length caching.