使用lodash来小写内部数组元素

man*_*ish 3 javascript lodash

我有这个对象

ob = {
 timePeriod: "Month",
 device: ["A", "B"]
}
Run Code Online (Sandbox Code Playgroud)

当我使用

x=_.mapValues(ob, _.method('toLowerCase'))
Run Code Online (Sandbox Code Playgroud)

x

 timePeriod: "month",
 device: undefined
Run Code Online (Sandbox Code Playgroud)

它不能小写device数组.

Jag*_*row 5

数组没有toLowerCase函数.改为以下

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"]}
Run Code Online (Sandbox Code Playgroud)