发布于 5年前

js删除对象上值无效的属性

function clearObject(obj) {
  for (const key in obj) {
    if (typeof obj[key] != 'number' && !obj[key]) {
      delete obj[key]
    }
  }
  return obj
}

let obj = clearObject({
  name: '',
  age: 1,
  status: 0,
  created_at: undefined,
  sex: null
})
// { age: 1, status: 0 }
©2020 edoou.com   京ICP备16001874号-3