admin管理员组

文章数量:1332395

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.

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.

Share Improve this question asked Feb 22, 2016 at 10:37 Manish KumarManish Kumar 10.5k26 gold badges82 silver badges156 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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"]}
var ob = {
    timePeriod: "Month",
    device: ["A", "B"]
}
var lowerCase = _.mapValues(ob, function(value){
    return _.isArray(value) ? _.map(value, _.toLowerCase) : _.toLowerCase(value);
})

本文标签: javascriptuse lodash to lowercase internal array elementStack Overflow