下面的js导出一个方法paramsTrim,传入一个参数params它的作用是将params里的值为string类型的去掉首位空格 不会改变params 返回新的params
- @param params 要处理的参数,必须为对象 或 数组
import { cloneDeep } from 'lodash-es'
export const paramsTrim = (params: Object | any[]) => {
const data = cloneDeep(params)
if (Object.prototype.toString.call(params) === '[object Array]') {
(data as any[]).forEach((key,idx) => {
if (Object.prototype.toString.call(key) === '[object Array]') {
data[idx] = paramsTrim(key)
} else if (Object.prototype.toString.call(key) === '[object Object]') {
data[idx] = paramsTrim(key)
} else if (Object.prototype.toString.call(key) === '[object String]')) {
data[idx] = data[idx].trim()
}
})
return data
} else {
const keysArr = Object.keys(data)
keysArr.forEach(key => {
if (Object.prototype.toString.call(data[key]) === '[object String]') {
data[key] = data[key].trim()
} else if (Object.prototype.toString.call(data[key]) === '[object Object]') {
data[key] = paramsTrim(data[key])
} else if (Object.prototype.toString.call(data[key]) === '[object Array]') {
data[key] = paramsTrim(data[key])
}
})
return data
}
}
|