从JavaScript对象中删除falsy值可以使用以下方法:
const obj = {
name: 'John',
age: 25,
address: null,
email: '',
active: false
};
for (let key in obj) {
if (!obj[key]) {
delete obj[key];
}
}
console.log(obj);
// 输出: { name: 'John', age: 25 }
const obj = {
name: 'John',
age: 25,
address: null,
email: '',
active: false
};
const filteredObj = Object.keys(obj).reduce((acc, key) => {
if (obj[key]) {
acc[key] = obj[key];
}
return acc;
}, {});
console.log(filteredObj);
// 输出: { name: 'John', age: 25 }
const _ = require('lodash');
const obj = {
name: 'John',
age: 25,
address: null,
email: '',
active: false
};
const filteredObj = _.pickBy(obj, Boolean);
console.log(filteredObj);
// 输出: { name: 'John', age: 25 }
以上方法可以帮助你从JavaScript对象中删除falsy值。对于JavaScript对象中的属性删除,腾讯云并没有提供特定的产品或服务。
领取专属 10元无门槛券
手把手带您无忧上云