i’m confused i was trying to remove object in object array using jquery here is my code , jsFiddle
var x = new Array() ;
var y = {} ;
y.name = 'myName' ;
y.age = 28 ;
y.phone = 27895556 ;
y.info = [{name:'x' ,age:58} ,{name:'y' , age:15}]
x.push(y) ;
$.each(x , function(index,value) {
$.each(value.info , function(i,v){
if(v.name == 'x'){
this.splice(i,1) ;
}
});
});
i was trying to tell the if condition to remove the object with v.name = ‘x’ but i get this error Uncaught TypeError: Object # has no method ‘splice’
UPDATE i need to have something like : y.info = [{name:'x' ,age:58} ,{name:'y' , age:15}] after splice()
any idea what i’m doing wrong Thanks
If you’re just trying to remove the inner array element that contains the value
{name: 'x'}then the array you want tospliceis thevalue.infoof the outer loop:However this code suffers from the problem that you shouldn’t modify an array’s length while iterating over it with
$.each. This alternate code fixes that problem: