Skip to content

keys

Get all keys from an object deeply

397 bytes

Usage

Given an object, return all of it’s keys and children’s keys deeply as a flat string list.

import * as _ from 'radashi'
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.

import * as _ from 'radashi'
_.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
// }

As of v10.5.0+ you can get this behavior via the crush function