美文网首页
去除数据内的无效值

去除数据内的无效值

作者: waiterYu | 来源:发表于2020-05-13 14:04 被阅读0次
效果
image.png
image.png
代码
function getValueObject(object) {
  if (Object.prototype.toString.call(object) === '[object Object]') {
    const obj = {}
    for (const key in object) {
      const value = getValueObject(object[key])
      if (isHaveValue(value)) {
        obj[key] = value
      }
    }
    return obj
  } else if (Array.isArray(object)) {
    return object.map(item => getValueObject(item))
  } else {
    return object
  }
}
function isHaveValue(value) {
  if (typeof value === 'undefined' || value === null || (typeof value === 'string' && value.trim() === '')) {
    return false
  }
  return true
}
let data=[{
  a:123,
  b:'',
  c:null,
  d:[]
}]
console.log(getValueObject(data));

相关文章

网友评论

      本文标题:去除数据内的无效值

      本文链接:https://www.haomeiwen.com/subject/xlennhtx.html