Reputation: 10492
I have this object
ob = {
timePeriod: "Month",
device: ["A", "B"]
}
when i use
x=_.mapValues(ob, _.method('toLowerCase'))
x
is
timePeriod: "month",
device: undefined
it is not able to lowercase device
array.
Upvotes: 4
Views: 6090
Reputation: 7406
var ob = {
timePeriod: "Month",
device: ["A", "B"]
}
var lowerCase = _.mapValues(ob, function(value){
return _.isArray(value) ? _.map(value, _.toLowerCase) : _.toLowerCase(value);
})
Upvotes: 2
Reputation: 7742
Array dont have toLowerCase function. Change to below
x = _.mapValues(ob, function(val) {
if (typeof(val) === 'string') {
return val.toLowerCase();
}
if (_.isArray(val)) {
return _.map(val, _.method('toLowerCase'));
}
});
JSON.stringify(x) // {"timePeriod":"month","device":["a","b"]}
Upvotes: 5