Manish Kumar
Manish Kumar

Reputation: 10492

use lodash to lowercase internal array element

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

Answers (2)

stasovlas
stasovlas

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

Jagdish Idhate
Jagdish Idhate

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

Related Questions