gpt4 book ai didi

javascript - 按空属性/条目过滤对象

转载 作者:行者123 更新时间:2023-11-28 06:25:05 26 4
gpt4 key购买 nike

如何过滤对象,例如:

{"firstname":"foo","lastname":"bar","driving":"","overnight":""}

{"firstname":"foo","lastname":"bar"}

仅删除空键/值对

最佳答案

使用 Object.keys() 和 Array.prototype.forEach() 的解决方案:

var original = {"firstname":"foo","lastname":"bar","driving":"","overnight":""};
var filtered = {}
Object.keys(original).forEach(function(key) {
if (original[key] !== "") {
filtered[key] = original[key];
}
});
console.log(filtered);

关于javascript - 按空属性/条目过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com