1

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); 

1
  • 1
    newObj[objCodes[i]] = originalObj[i] Commented Jun 25, 2020 at 7:32

3 Answers 3

1

Just change the line like in the snippet below

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);

Sign up to request clarification or add additional context in comments.

Comments

1
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);

Change the dotted notation to bracket notation. Reason for this is JavaScript allows only valid names with dotted notation which cannot start with numeric value.And In your case the keys are set to be 101,102...and so on, which are invalid.

Edit : Dynamic property names can be used only through bracket notations, such as in your case where the property name is set using a variable.

1 Comment

Because the newObj.objCodes[i] will not work regardless of what the value of objCodes[i] is, numerical values has nothing to do with it. You need newObj[objCodes[i]] because objCodes[i] is a variable and the newObj key is being set dynamically.
0

You can get the keys, iterate them and copy the items like this:

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 = {};

Object.keys(objCodes).forEach(i => newObj[objCodes[i]] = originalObj[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.