I want to update (replace) the objects in my array with the objects in another array. Each object has the same structure. e.g.
var origArr = [
This will do what you need:
var origArr = [
{name: 'Trump', isRunning: true},
{name: 'Cruz', isRunning: true},
{name: 'Kasich', isRunning: true}
];
var updatingArr = [
{name: 'Cruz', isRunning: false},
{name: 'Kasich', isRunning: false}
];
for (var i = 0; i < updatingArr.length; ++i) {
var updateItem = updatingArr[i];
for (var j = 0; j < origArr.length; ++j) {
var origItem = origArr[j];
if (origItem.name == updateItem.name) {
origItem.isRunning = updateItem.isRunning;
break;
}
}
}
document.write('' + JSON.stringify(origArr, 0, 4) + '
');