4

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.

2 Answers 2

5

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"]}
Sign up to request clarification or add additional context in comments.

Comments

2
var ob = {
    timePeriod: "Month",
    device: ["A", "B"]
}
var lowerCase = _.mapValues(ob, function(value){
    return _.isArray(value) ? _.map(value, _.toLowerCase) : _.toLowerCase(value);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.