ts (js) general method, remove the leading and trailing spaces when submitting to the backend

The following js exports a methodparamsTrim, passing in a parameterparamsWhat it does is to convert theparamsThe value in the string type removes the first space will not change params and returns a new oneparams

  • @param params The parameters to be processed must be objects or arrays
import {
    
     cloneDeep } from 'lodash-es'

/**
 * @info 将params里的值为string类型的去掉首位空格 不会改变params 返回新的params
 * @param params 要处理的参数,必须为对象 或 数组
 * @returns 处理后的params
 */
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
  }
}

Guess you like

Origin blog.csdn.net/weixin_44441196/article/details/123402224