I use extract method for my project. Purpose can be different. These codes from my localization project on Github.
github.com/aligoren/local.js/blob/...
extract(propertyName, object) { const parts = propertyName.split("."); let length = parts.length; let i; let property = object || this; for (i = 0; i < length; i++) { property = property[parts[i]]; } return property; }
Usage:
const locals = { "tr": { "btn": { "welcome": { "text": "Merhaba hoşgeldin" }, "other": "Diğeri" } }, "en": { "btn": { "welcome": { "text": "Hi Welcome" }, "other": "Other" } } } this.extract('btn.welcome.text', locals['tr'])
This is great and easily readable. Maybe we should benchmark all the ways to access nested objects and spread the word so the community will know which one to use and when.
Take a look at i18n library, works in this way.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I use extract method for my project. Purpose can be different. These codes from my localization project on Github.
github.com/aligoren/local.js/blob/...
Usage:
This is great and easily readable. Maybe we should benchmark all the ways to access nested objects and spread the word so the community will know which one to use and when.
Take a look at i18n library, works in this way.