Skip to content

Commit 01c2c83

Browse files
✏️ Fix spelling mistake in description and update filterObject function to handle null and undefined values
1 parent 8935d03 commit 01c2c83

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

public/data/javascript.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,26 @@
121121
"snippets": [
122122
{
123123
"title": "Filter Object",
124-
"description": "Filters out entries in an object where the value is falsy.",
124+
"description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
125125
"code": [
126126
"export const filterObject = (object = {}) =>",
127127
" Object.fromEntries(",
128128
" Object.entries(object)",
129-
" .map(([key, value]) => value && [key, value])",
130-
" .filter((item) => item),",
129+
" .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))",
131130
" );",
132131
"",
133132
"// Usage:",
134-
"const obj = { a: 1, b: null, c: undefined, d: 4 };",
135-
"console.log(filterObject(obj)); // Output: { a: 1, d: 4 }"
133+
"const obj1 = { a: 1, b: null, c: undefined, d: 4, e: '', f: {} };",
134+
"console.log(filterObject(obj1)); // Output: { a: 1, d: 4 }",
135+
"",
136+
"const obj2 = { x: 0, y: false, z: 'Hello', w: [] };",
137+
"console.log(filterObject(obj2)); // Output: { z: 'Hello' }",
138+
"",
139+
"const obj3 = { name: 'John', age: null, address: { city: 'New York' }, phone: '' };",
140+
"console.log(filterObject(obj3)); // Output: { name: 'John', address: { city: 'New York' } }",
141+
"",
142+
"const obj4 = { a: 0, b: '', c: false, d: {}, e: 'Valid' };",
143+
"console.log(filterObject(obj4)); // Output: { e: 'Valid' }"
136144
],
137145
"tags": ["javascript", "object", "filter", "utility"],
138146
"author": "realvishalrana"

0 commit comments

Comments
 (0)