Re-creating the Lodash library - _.invert()

Hey there everyone!

I’m at this project and I’m unable to grasp the solution for the invert method. I don’t get how the iterator for… in loop is swapping key: values without using the method .keys. It seems quite odd to me!
here’s the solution which I’ve found through trial and error:

  invert: function(obj){
    let invertedObject = {}
    for (key in obj.keys) {
      let originalValue = obj[key];
      invertedObject = {originalValue: key};
    }
    return invertedObject;
  }

and this is my code:

  invert: function(obj){
    let invertedObject = {}
    for (key in obj.keys) {
      let originalValue = obj[key];
      invertedObject[originalValue] = key;
    }
    return invertedObject;
  }

I do get now why the object should be in the format {originalValue, key}, but I don’t get how the key was accessed. Also, what if the key was a string and not sequential numbers? Does the word key in the for … in loop functions as a keyword to access the key? I thought using it would be the same as using the word k for example.

Thanks!
:baby:

but I don’t get how the key was accessed

Through the for-loop iterator. for (x in list) will iterate through every item in list. x being the place-holder of the cycled items in list for each loop.

what if the key was a string and not sequential numbers?

As long as it was an iterable object of strings, it would have cycled through the strings. If it’s just a string (sometimes useful), then you’d get the index number of each letter (different from python, where you just get the letters!).

for (i in 'string'){
	console.log(i);
}
//0
//1
//2
//3
//4
//5

for (i in 'string'){
	console.log('string'[i]);
}
//s
//t
//r
//i
//n
//g

Does the word key in the for … in loop functions as a keyword to access the key?

Yes, it can be any word in theory, but for readability it should reflect what’s being iterated. (i for item, el for element, etc). K would work.

:thinking: I just learned something interesting doing this… (since python is my main language) :sweat_smile:

1 Like

Haha! That’s nice!
But if I iterate through a key:value pair, wouldn’t it print the key:value pair? Or just the key? I’ve tested it out and it only prints the keys! That’s what I don’t get!

for (key in {key1: 'value1', key2:'value2'}){
	console.log(key);
}
//key1
//key2

Thanks Pita!

I’ve got to say that this exercise has been pretty awesome! Props to codecademy

1 Like

Are you thinking about this?

let x = {key1: 'value1', key2:'value2'}

for (key in x){
	console.log(x[key]);
}

//value1
//value2

Well this was my solution:

     invert(obj) {
         let objKeys = Object.keys(obj);
         let newObj = {}
         for(let i = 0; i < objKeys.length; i ++) {
             let holding = obj[objKeys[i]];
             newObj[holding] = objKeys[i];            
         }
         return newObj;
    }

But then after playing around with that for in loop just above, managed to shrink it down to the following with the tests still passing. I need to keep refreshing myself on all of the different loops

invert(obj) {
        let newObj = {};
        for (key in obj) {
            newObj[obj[key]] = key;
        }
        return newObj;
    }