过滤掉以下数组中的空对象(此处特指有key无value 的对象):
let array = [
{name: 'a ',age: '12 '},
{name: 'b ',age: ' 18'},
{name: '',age: ''},
{name: 'f ',age: ' 18'}
]
解决办法:
function isEmpty(obj) {
let empty = true;
for (let key in obj) {
if (obj[key]) {
empty = false;
break;
}
}
return empty
}
function filter(array) {
return array.filter( item => !isEmpty(item))
}
let newArr = filter(array);
console.log(newArr)