I'm working on a challenge that requires me to implement the .invert() method myself. Below is my solution but the expected outcome doesn't match with the actual outcome returned. Please see the second block below.
Implemented .invert() method:
const _ = {
invert(object) {
let newObject = {};
for (let key in object) {
const originalValue = object[key]
newObject = {
originalValue: key
}
}
return newObject;
}
};
Tested with below code:
var obj1 = { 'a': '1', 'c': '1', 'b': '2' };
console.log(_.invert(obj1));
And I expected to see below is returned:
{ '1': 'c', '2': 'b' }
Actually returned value was as below:
{ originalValue : 'c'}
I'm doing something terribly wrong, but can't figure out what... Can someone help?
newObject = {originalValue: key}
should benewObject = {[originalValue]: key}
. See documentation for computed properties: tylermcginnis.com/computed-property-names