Personally, I would do it like this.
function renameKeys(arr, nameMap) {
// loop around our array of objects
for(var i = 0; i < arr.length; i++) {
var obj = arr[i];
// loop around our name mappings
for(var j = 0; j < nameMap.length; j++) {
var thisMap = nameMap[j];
if(obj.hasOwnProperty(thisMap.from)) {
// found matching name
obj[thisMap.to] = obj[thisMap.from];
delete obj[thisMap.from];
}
}
}
}
You would call it like so, where myArray is your array of objects.
renameKeys(myArray, [
{from: "id", to: "num" },
{ from: "name", to: "fullname" }
]);
Has the advantage that it is reusable for any number of name re-mappings. Doesn't modify native prototypes. And only iterates once around the array, no matter how many re-mappings take place.