Skip to content

Instantly share code, notes, and snippets.

@ahmehri
Last active December 4, 2019 16:52
Show Gist options
  • Save ahmehri/4c520e694826f627813d4203c45fbd34 to your computer and use it in GitHub Desktop.
Save ahmehri/4c520e694826f627813d4203c45fbd34 to your computer and use it in GitHub Desktop.
const { keyBy, mapKeys } = require("lodash");
let result;
const current = {
firstName: "Ahmed",
lastName: "Mehri",
age: 30
};
const expected = {
Ahmed: {
firstName: "Ahmed",
lastName: "Mehri",
age: 30
}
};
// doesn't work either (wrong usage of mapKeys)
result = mapKeys(current, "firstName"); // { undefined: 30 }
// doesn't work either (correct usage of mapKeys)
result = mapKeys(current, (value, key) => value); // { 30: 30, Ahmed: 'Ahmed', Mehri: 'Mehri' }
// does not work
result = keyBy(current, "firstName"); // { undefined: 30 }
// works only on collection
const collection = [current];
result = keyBy(collection, "firstName"); // { Ahmed: { firstName: 'Ahmed', lastName: 'Mehri', age: 30 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment