Skip to content

Instantly share code, notes, and snippets.

@ikasoba
Last active March 4, 2022 10:12
Show Gist options
  • Save ikasoba/710a35b2947028b721734e471116d8ff to your computer and use it in GitHub Desktop.
Save ikasoba/710a35b2947028b721734e471116d8ff to your computer and use it in GitHub Desktop.
再帰的にプロパティを取ってくるくん 1号
/**
* @return {any|null|undefined}
* @example
* getProperty(
* "posts[0].content",
* {
* posts:[
* {
* content:"hogehoge"
* }
* ]
* }
* )
* // "hogehoge"
*/
export const getProperty = (o,oo)=>typeof o === "string" ? getProperty(o.replace(/\[([0-9]+)\]/g,(_,i)=>"."+i).split("."),oo) : (o?.length==0) ? oo : getProperty(o.slice(1),oo[o[0]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment