效果
image.pngimage.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));
网友评论