jmenezes
jmenezes

Reputation: 1926

Creating a new javascript object based on another object

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

Upvotes: 1

Views: 77

Answers (3)

Lajos Arpad
Lajos Arpad

Reputation: 77118

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

Upvotes: 0

Shreyash bhatt
Shreyash bhatt

Reputation: 156

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.

Upvotes: 1

RacoonOnMoon
RacoonOnMoon

Reputation: 1586

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

Upvotes: 1

Related Questions