I'm trying to create a new javascript object that'll have new key names based on another object. I'm almost there, but my code fails at the line // fails here with Uncaught TypeError: Cannot set property 'name' of undefined . Any idea how to get this right? Also, is there a more efficient way to build a new object in this case? I need it work on older IE browsers, hence this approach.
originalObj = {"name":"John","age":30,"state":"CA","country":"USA"};
objCodes = {"name":101,"age":102,"state":103,"country":104};
// newObj = {101:"John", 102:30,103:"CA",104:"USA"};
newObj = {};
for (var i in originalObj) {
if (objCodes.hasOwnProperty(i)) {
// console.log(i, originalObj[i]);
console.log(objCodes[i],originalObj[i])
newObj.objCodes[i] = originalObj[i] // fails here
}
}
console.log(newObj);
newObj[objCodes[i]] = originalObj[i]