Can you explain the diffrence between two javascript code snippets below?

Viewed 99

i was asked to write out a piece of code that inverts all key value pairs from an object passed in. this is what i wrote out.

invert(object){
   let newObj = {};
   
   for(let key in object){
      const original = object[key];
      newObj = {original : key}
   }
   
   return newObj;        
}

aparently that dosent work ( and i cant figure out why). the answer to the question was

invert(object){
   let invertedObject = {};
   
   for(let key in object){
      const originalValue = object[key];
      invertedObject = {originalValue : key}
   }
 
   return invertedObject;
}

when ran with with a test file, my code fails while the answer code passes. reasoning was mine is returning undefined.

error code from test :

Failed: _.invert({originalKey: "originalValue"})["originalValue"]) returned undefined instead of originalKey

2 Answers

Difference is in the following statement:

newObj = {original : key}

In your code, when you use original as a key in the newObj, instead of using the value of the original variable as a key, 'original' is used literally as a key.

The original variable is unused in your code.

You can fix the problem by using computed property name as:

newObj = { [original]: key };

Without using the computed property, newObj will be:

{
   original:"originalKey"
}

but with computed property name, newObj will be:

{
   originalValue:"originalKey"
}

The second code example works because in the following statement

invertedObject = {originalValue : key}

they have used the value of the originalKey as the name of the key. So the key of the returned object will be 'originalValue'.

Also note that the following statement in the second code example

const originalValue = object[key];

is unnecessary because originalValue variable is not being used. Second code example will also work without the above statement.

If second code example uses computed property name as:

invertedObject = { [originalValue] : key }

then you need the statement that declares the originalValue variable.


Personally, I think second code example is error prone and is really easy to break; its also not practical for making a reusable function because:

  • objects passed to this function won't always have a single key
  • you won't always know the value of each key in the object and even if you do know the values, if the object has multiple keys, this approach won't work

You should use computed property name which doesn't depends on a variable name being the same as the value of a key in the object.

that inverts all key value pairs from an object passed in

You have to add each property to the same object and not create new object for each property.

function invert(object) {
  let newObj = {};
  for(let key in object){
      const original = object[key];
      newObj[original] = key;  // add property to same 'newObj' object
  }
      return newObj;        
}

console.log(invert({originalKey: "originalValue", anotherKey: "anotherValue"}))

Related