Lodash invert method

Sorry this is quit embarrassing, I’m stuck on the invert method and even went through the video but for some reason I still can’t get everything to pass.

this is the invert method

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

I just can’t get the pass for this …

4 - Returns an object with all keys and values inverted - Failed: _.invert({originalKey: “originalValue”})[“originalKey”]) returned undefined instead of anotherKey.

I’ve even tried it on vsCode with this:

object ={
name: ‘Bob’,
age: ‘26’,
learning: ‘Coding’
}

const invertedObject = (object) => {
let inverted = {};
for(let key in object){
const currentValue = object[key]
inverted = {currentValue : key}
}
return inverted;
}

console.log(invertedObject(object))

the currentValue and object[key] does give the right output but it seems to be the next line that the currentValue is treating it like a string…
the currentValue in const currentValue = object[key] says its stated but not read elsewhere … doesn’t seem to be picking up the currentValue inside the object?

Thank you!

:slight_smile:

1 Like

Hello, @cindycheung.

Could you tell me how do we add new properties to an object?
For example, if I have an object such as

var someInvertedObject = {
  property1: "value1"
}

So, if I want to add a property called property2 which is a string 'value2' to the someInvertedObject, how would I do that?

1 Like

Hi @nova-xronos !

Thank you so much for your hint! It was really helpful! :smiley:

I did someInvertedObject[property2]: value2 and applied it to the question and it’s all passed :smiley:

Thank you!

1 Like