I can use lodash to access nested keys inside Javascript objects:
type Cat = {
name: string
human: Human
}
type Human = {
name: string
}
const sophie: Cat = {name: 'Sophie', human: {name: 'Courtney'}}
>> console.log(_.get(sophie, 'human.name'))
Courtney
What if I the nested property I want is inside an array?
import {magicNestedGet} from 'unicorn'
type Cat = {
name: string
humans: Human[]
}
const sophie: Cat = {name: 'Sophie', humans: [{name: 'Courtney'}, {name: 'Lukas'}]}
>> console.log(magicNestedGet(sophie, 'humans[].name')) // or maybe 'humans.name[]' ???
[Courtney, Lukas]
I thought lodash did this but I can't get it to work. Is there a library that supports a syntax like this?