去除数据内的无效值
作者:
waiterYu | 来源:发表于
2020-05-13 14:04 被阅读0次效果
![](https://img.haomeiwen.com/i13351496/2091eb3fc0a941ff.png)
image.png
![](https://img.haomeiwen.com/i13351496/b97b37f2b3aa83a5.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
网友评论