How can I change the value of an element in object array? My example below removes one object from an array and then change the element (number) in the rest of the object – the removing part works fine but the other part doesn’t-
I have a jsFiddle
code :
var x =[
{name : 'myname' , number : '10' , color:'green'},
{name : 'yourname' , number : '15' , color:'blue'}
];
$.each(x , function(index ,value) {
if(value.number == '10'){
x.splice(index , 1) ;
}
else {
x[i].number = '20' ;
}
console.log(x) ;
});
You can use
.map()to build a new array like this
Demo: http://jsfiddle.net/joycse06/C3d9T/3/