Lodash - invert method


#1

I’m not getting this result:

var object = { ‘a’: 1, ‘b’: 2, ‘c’: 1 };
_.invert(object);
// => { ‘1’: ‘c’, ‘2’: ‘b’ }

Can someone help me with this, please?


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

We cannot declare a constant in a loop. That will throw an error.


#3

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash

I have the same issue. I have the same exact code because in step 25.5 they ask us to create within the loop a variable…

invert(object){
const invertedObject = {};

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

},


#4
for (var key in object){
  var originalValue = object[key];
  invertedObject= {
    originalValue: key
  };
}

invertedObject cannot be redeclared in an assignment. We can however assign new key-value pairs.

object[key] = value;