Last active
March 4, 2022 10:12
-
-
Save ikasoba/710a35b2947028b721734e471116d8ff to your computer and use it in GitHub Desktop.
再帰的にプロパティを取ってくるくん 1号
This file contains hidden or 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
/** | |
* @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