diff --git a/.zk/notebook.db b/.zk/notebook.db index 70ae8fb..6e583fb 100644 Binary files a/.zk/notebook.db and b/.zk/notebook.db differ diff --git a/zk/Concise_mapping_of_object_subfileds_in_JS.md b/zk/Concise_mapping_of_object_subfileds_in_JS.md deleted file mode 100644 index 1fd8850..0000000 --- a/zk/Concise_mapping_of_object_subfileds_in_JS.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -id: dv3u -title: Concise mapping of object subfileds in JS -tags: [] -created: Friday, June 28, 2024 ---- -# Concise mapping of object subfileds in JS - - -## Related notes - - diff --git a/zk/Concise_subfield_mapping_JS.md b/zk/Concise_subfield_mapping_JS.md new file mode 100644 index 0000000..4bd6e61 --- /dev/null +++ b/zk/Concise_subfield_mapping_JS.md @@ -0,0 +1,38 @@ +--- +id: dv3u +title: Concise mapping of object subfileds in JS +tags: [] +created: Friday, June 28, 2024 +--- + +# Concise mapping of object subfileds in JS + +## Scenario + +You have an array of objects and you want to return the objects with only a +subset of the fields. + +## Implementation + +Standard approach with a map: + +```js +const arrayOfObjs = [ + { id: 12, name: "Thomas" }, + { id: 3, name: "Gerald" }, +]; + +// We just want the `name` property + +const subset = arrayOfObjs.map((obj) => { + name: obj.name; +}); +``` + +More concise approach with destructuring: + +```js +const subset = arrayOfObjs.map(({ name }) => ({ name })); +``` + +## Related notes