-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add keys function to object module (#226)
* add keys function to object module
- Loading branch information
Showing
9 changed files
with
181 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: keys | ||
description: Get all keys from an object deeply | ||
group: Object | ||
--- | ||
|
||
## Basic usage | ||
|
||
Given an object, return all of it's keys and children's keys deeply as a flat string list. | ||
|
||
```ts | ||
import { keys } from 'radash' | ||
|
||
const ra = { | ||
name: 'ra', | ||
power: 100, | ||
friend: { | ||
name: 'loki', | ||
power: 80 | ||
}, | ||
enemies: [ | ||
{ | ||
name: 'hathor', | ||
power: 12 | ||
} | ||
] | ||
} | ||
|
||
keys(ra) | ||
// => [ | ||
// 'name', | ||
// 'power', | ||
// 'friend.name', | ||
// 'friend.power', | ||
// 'enemies.0.name', | ||
// 'enemies.0.power' | ||
// ] | ||
``` | ||
|
||
This is a function you might like to use with `get`, which dynamically looks up values in an object given a string path. Using the two together you could do something like flatten a deep object. | ||
|
||
```ts | ||
import { keys, get, objectify } from 'radash' | ||
|
||
objectify( | ||
keys(ra), | ||
key => key, | ||
key => get(ra, key) | ||
) | ||
// => { | ||
// 'name': 'ra' | ||
// 'power': 100 | ||
// 'friend.name': 'loki' | ||
// 'friend.power': 80 | ||
// 'enemies.0.name': 'hathor' | ||
// 'enemies.0.power': 12 | ||
// } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ export { | |
clone, | ||
get, | ||
invert, | ||
keys, | ||
listify, | ||
lowerize, | ||
mapEntries, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3975ea5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
radash-docs – ./
radash-docs.vercel.app
radash-docs-ray-unishinedev.vercel.app
radash-docs-git-master-ray-unishinedev.vercel.app