Se dobbiamo supportare browser obsoleti, i più recenti metodi di manipolazione degli array JavaScript non sono disponibili.
Il codice è il seguente:
var arrObjects = [
{
id: 1,
bar: 'Test'
},
{
id: 2,
bar: 'Foo'
},
{
id: 3,
bar: 'Baz'
}
];
var index = 0;
for(var i = 0; i < arrObjects.length; i++) {
var obj = arrObjects[i];
if(obj.id === 2) {
index = i;
break;
}
}
arrObjects[index] = {
id: 4,
bar: 'Baz'
};
// [{id: 1, bar: 'Foo'},{id: 4, bar: 'Baz'}, {id: 3, bar: 'Baz'}]