Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 56826  / 8 Years ago, sun, november 20, 2016, 12:00:00

I have something like


data = {
'id':'123',
'employee_name': 'John',
'employee_type': 'new'
}

var newObj = _.mapValues(data, function (value, key) {
var t = _.camelCase(key);
console.log(t) -> shows employeeName and employeeType

return _.camelCase(key);
});

I was expecting my newObj will become


data = {
'id':'123',
'employeeName': 'John',
'employeeType': 'new'
}

More From » lodash

 Answers
11

Use _.mapKeys() instead of _.mapValues():





var data = {
'id': '123',
'employee_name': 'John',
'employee_type': 'new'
};

var newObj = _.mapKeys(data, (value, key) => _.camelCase(key));

console.log('newObj: ', newObj);

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js></script>





If you need to ignore the redundant value param, you can use _.rearg() on _.camelCase() to generate a function that takes the 2nd param (the key) instead of the 1st param (the value).





var data = {
'id': '123',
'employee_name': 'John',
'employee_type': 'new'
};

var newObj = _.mapKeys(data, _.rearg(_.camelCase, 1));

console.log('newObj: ', newObj);

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js></script>




[#59987] Friday, November 18, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;