How to use the mapKeys function from lodash

Find comprehensive JavaScript lodash.mapKeys code examples handpicked from public code repositorys.

lodash.mapKeys creates a new object with the same values as the original object, but with keys transformed by the specified function.

139
140
141
142
143
144
145
146
147
148
149
    entry.name = name;
    return entry;
}


function to_xattr(fs_xattr) {
    const xattr = _.mapKeys(fs_xattr, (val, key) =>
        (key.startsWith(XATTR_USER_PREFIX) ? key.slice(XATTR_USER_PREFIX.length) : '')
    );
    // keys which do not start with prefix will all map to the empty string key, so we remove it once
    delete xattr[''];
fork icon68
star icon228
watch icon17

+ 3 other calls in file

248
249
250
251
252
253
254
255
256
257
module.exports.lte                 = _.lte;
module.exports.lteContrib          = _.lteContrib;
module.exports.map                 = _.map;
module.exports.mapArgs             = _.mapArgs;
module.exports.mapArgsWith         = _.mapArgsWith;
module.exports.mapKeys             = _.mapKeys;
module.exports.mapValues           = _.mapValues;
module.exports.mapcat              = _.mapcat;
module.exports.matches             = _.matches;
module.exports.matchesProperty     = _.matchesProperty;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.mapKeys work?

lodash.mapKeys is a function that creates a new object by applying a mapping function to each key of a source object, while maintaining the same value associated with the key. The mapping function is applied to each key-value pair, and the resulting key-value pairs are used to create a new object.

52
53
54
55
56
57
58
59
60
61
62
63
64
};


// Same as above, but where the keys are just the number at the front. These
// are used when looking up which slots correspond to which vaccine in an API
// response.
const VACCINE_IDS_SHORT = mapKeys(VACCINE_IDS, (_, key) => key.split("-")[0]);


const warn = createWarningLogger("riteAidScraper");


async function queryZipCode(zip, radius = 100, stores = null) {
fork icon2
star icon7
watch icon7

+ 4 other calls in file

677
678
679
680
681
682
683
684
685
686
687
688
689
console.log(keys); // => ['a', 'b']


const keysIn = _.keysIn({ 'a': 1, 'b': 2 });
console.log(keysIn); // => ['a', 'b']


const mapKeys = _.mapKeys({ 'a': 1, 'b': 2 }, (value, key) => key + value);
console.log(mapKeys); // => { 'a1': 1, 'b2': 2 }


const mapValues = _.mapValues({ 'a': 1, 'b': 2 }, (value, key) => key + value);
console.log(mapValues); // => { 'a': 'a1', 'b': 'b2' }
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
const obj = { a: 1, b: 2, c: 3 };
const mappedObj = _.mapKeys(obj, (value, key) => key.toUpperCase());
console.log(mappedObj); // { A: 1, B: 2, C: 3 }

In this example, lodash.mapKeys is used to map over the keys of an object obj and convert them to uppercase using a mapping function. The resulting object mappedObj has the same values as obj, but with uppercase keys.

172
173
174
175
176
177
178
179
180
181
static *refresh(quest, range) {
  /* The result is an array, we must correct the keys according to the
   * offset (first index).
   */
  const ids = Object.values(
    _.mapKeys(
      Object.assign(
        {},
        yield* this._ids(quest, {
          start: range[0],
fork icon0
star icon3
watch icon2

+ 3 other calls in file

530
531
532
533
534
535
536
537
538
539
        }
    }
])

// Turn array of attendances into an object with date as keys: "2020-12-31"
attendances = lodash.mapKeys(attendances, (a) => {
    return moment(a.createdAt).format('YYYY-MM-DD')
})

if (padded) {
fork icon0
star icon1
watch icon0

+ 20 other calls in file

835
836
837
838
839
840
841
842
843
844
845
846


function replaceSpacesInSchemaNames(swagger) {
  if (_.isUndefined(swagger.definitions))
    return;


  swagger.definitions = _.mapKeys(swagger.definitions, function (value, key) {
    return replaceSpaces(key);
  });
}

fork icon0
star icon0
watch icon5

+ 3 other calls in file

70
71
72
73
74
75
76
77
78
79
  return { env, missingEnvs, presentEnvs };
}
const envCase = (string) => _.snakeCase(string).toUpperCase();
const unEnvCase = _.camelCase;
function envKeys(dict) {
  return _.mapKeys(dict, (value, key) => envCase(key));
}
function unEnvKeys(dict) {
  return _.mapKeys(dict, (value, key) => unEnvCase(key));
}
fork icon0
star icon0
watch icon1

1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
aggregatedRes.nomination['fields'] = _.map(data.request.fields, (field) => {
  const obj = {name: field};
  if (field === 'status') {
    obj['fields'] = {}
    const temp = _.groupBy(groupData[field]);
    _.mapKeys(temp, (val, key) => {
      obj.fields[key] = val.length
    })
  }else {
    obj['count'] = groupData[field].length;
fork icon0
star icon0
watch icon1

+ 3 other calls in file

85
86
87
88
89
90
91
92
93
});

describe('columnInfo with wrapIdentifier and postProcessResponse', () => {
  before('setup hooks', () => {
    knex.client.config.postProcessResponse = (response) => {
      return _.mapKeys(response, (val, key) => {
        return _.camelCase(key);
      });
    };
fork icon0
star icon0
watch icon1

+ 5 other calls in file

325
326
327
328
329
330
331
332
333
334
  },
  {}
);

// 精英化 & 技能
const skillId2Name = _.mapKeys(
  _.mapValues(
    _.omitBy(skillTable, (v, k) => k.startsWith('sktok_')),
    ({ levels }) => levels[0].name
  ),
fork icon0
star icon0
watch icon1

+ 14 other calls in file

192
193
194
195
196
197
198
199
200
201
for (const attrName of this.model.modelDefinition.attributes.keys()) {
  attrsMap[attrName.toLowerCase()] = attrName;
}

result = rows.map(row => {
  return _.mapKeys(row, (value, key) => {
    const targetAttr = attrsMap[key];
    if (typeof targetAttr === 'string' && targetAttr !== key) {
      return targetAttr;
    }
fork icon0
star icon0
watch icon0

592
593
594
595
596
597
598
599
600
601
//   a: 2,
//   b: 5,
//   c: 50,
// };
// console.log(
//   lodash.mapKeys(obj, (val, key) => {
//     return key + "some";
//   })
// );
// console.log(
fork icon0
star icon0
watch icon0

+ 2 other calls in file

151
152
153
154
155
156
157
158
159
160
    internal_notes__c: 'internalNotes',
    metadata__c: 'metadata'
  };


  const response = _.mapKeys(data, function (value, key) {
    return keyMap[key];
  });
  return response;
},
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)