Lodash Library - .invert() method

Hello,

I am really struggling to write the .invert() method in the Lodash library project. The invert method accepts an object as an argument, then it should loop through the key/value pairs of that object and then invert those key/value pairs and put them into an empty object within the same method. Essentially the new object that gets returned should have all the argument object’s keys as the value pairs, and the values as keys.

Any help much appreciated. I am lost.

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

Is there anything in particular stopping you?
When you ask for general help about the whole thing then presumably you haven’t had very many thoughts on it yet or there would be more specific things to ask about… So, what do you get as input, what can you do with that, and what out of those things might be useful in moving closer to the output? Or maybe you mean that you haven’t understood what’s being asked for?

sorry, should have been more specific.

I am trying to iterate through the object’s keys, then try to create a variable ‘originalValue’ and set it equal to the value on the current object’s key. From there I am lost and don’t really understand the next steps to set the value on ‘originalValue’ on the empty object ‘invertedObject’ to be the current key.
I have thought about it a lot, but my brain is fried at this point. If any one has any pointers on how to proceed I would be grateful.

invert(object){
invertedObject = {};
for(const x in object){
let originalValue = object;
};
return invertedObject;
},

If you have a key and a value you’d add that to the result wouldn’t you?
And there would be nothing else to do after that, so what other steps?
Copy the pairs from one object to another. And flip them while doing so.

2 Likes

Thanks for the pointers!

I fill all of the recommend they have provide for me to build this project here is on lodash.js. But I get error like this.

inverted_object_fail

The final outcome will only be the execution of that line. The returned object will be a ghost of its original self. We’re on the right track in terms of switching the two parameters around; the progressive assignments need a different take.

H[V] = K

Don’t understand sorry for that .Can you please repeat it easily again?

The inverted object that you return will only have one key-value pair, the last one declared/assigned. The loop is not constructing an object with multiple keys, only one key.

We cannot assign an object on each pass; we must assign a new key with the latest value.

invertedObject[originalValue] = key

Thank you very much .When I was stuck during this project I saw this video that codecademy provided in right-low corner site named unstuck. But in this video she provided the same things that I did and surprisingly she got the result what she wanted .

1 Like