Reputation: 1566
I am having a little bit of trouble of trying to filter out data that does not contain zero.
for example:
const height = {
cm: 0,
feet: 10,
inches: 5,
}
and the desired result is to remove elements that dont have zero.
so desired result is:
const height = {
feet: 10,
inches: 5,
}
i have tried
const boo = Object.keys(height).filter((e) => height[e] > 0);
but no luck.
ANy ideas
Upvotes: 3
Views: 950
Reputation: 2041
Object.keys(height).forEach(key => {
if (height[key] === 0) {
delete height[key]
}
})
Upvotes: 0
Reputation: 386654
You could iterate the entries and delete unwanted keys.
const height = { cm: 0, feet: 10, inches: 5 };
Object.entries(height).forEach(([k, v]) => {
if (v === 0) delete height[k];
});
console.log(height);
Another approach creates a new object without unwanted parts.
const
height = { cm: 0, feet: 10, inches: 5 },
result = Object.fromEntries(Object
.entries(height)
.filter(([k, v]) => v !== 0)
);
console.log(result);
Upvotes: 3