Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

✨ Add utility functions and snippets to javascript.json #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
✏️ Fix spelling mistake in description and update filterObject functi...
...on to handle null and undefined values
  • Loading branch information
realvishalrana committed Dec 30, 2024
commit 01c2c83eabe54419e58325f6a8d5db93b887092f
18 changes: 13 additions & 5 deletions public/data/javascript.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,26 @@
"snippets": [
{
"title": "Filter Object",
"description": "Filters out entries in an object where the value is falsy.",
"description": "Filter out entries in an object where the value is falsy, including empty strings, empty objects, null, and undefined.",
"code": [
"export const filterObject = (object = {}) =>",
" Object.fromEntries(",
" Object.entries(object)",
" .map(([key, value]) => value && [key, value])",
" .filter((item) => item),",
" .filter(([key, value]) => value !== null && value !== undefined && value !== '' && (typeof value !== 'object' || Object.keys(value).length > 0))",
" );",
"",
"// Usage:",
"const obj = { a: 1, b: null, c: undefined, d: 4 };",
"console.log(filterObject(obj)); // Output: { a: 1, d: 4 }"
"const obj1 = { a: 1, b: null, c: undefined, d: 4, e: '', f: {} };",
"console.log(filterObject(obj1)); // Output: { a: 1, d: 4 }",
"",
"const obj2 = { x: 0, y: false, z: 'Hello', w: [] };",
"console.log(filterObject(obj2)); // Output: { z: 'Hello' }",
"",
"const obj3 = { name: 'John', age: null, address: { city: 'New York' }, phone: '' };",
"console.log(filterObject(obj3)); // Output: { name: 'John', address: { city: 'New York' } }",
"",
"const obj4 = { a: 0, b: '', c: false, d: {}, e: 'Valid' };",
"console.log(filterObject(obj4)); // Output: { e: 'Valid' }"
],
"tags": ["javascript", "object", "filter", "utility"],
"author": "realvishalrana"
Expand Down

AltStyle によって変換されたページ (->オリジナル) /