Autosave: 2024-06-28 09:45:03
This commit is contained in:
parent
0fffdeb8b8
commit
76b7472c01
3 changed files with 38 additions and 12 deletions
BIN
.zk/notebook.db
BIN
.zk/notebook.db
Binary file not shown.
|
|
@ -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
|
|
||||||
|
|
||||||
|
|
||||||
38
zk/Concise_subfield_mapping_JS.md
Normal file
38
zk/Concise_subfield_mapping_JS.md
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Reference in a new issue