https://github.com/writetome51/get-property
Allows you to retrieve value of object property using dot-notation in a string
https://github.com/writetome51/get-property
javascript object properties property
Last synced: 6 months ago
JSON representation
Allows you to retrieve value of object property using dot-notation in a string
- Host: GitHub
- URL: https://github.com/writetome51/get-property
- Owner: writetome51
- License: mit
- Created: 2019-02-02T08:00:52.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-08-27T23:00:53.000Z (over 5 years ago)
- Last Synced: 2025-07-25T06:43:21.011Z (6 months ago)
- Topics: javascript, object, properties, property
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getProperty(property: string, object): any
Why would you use this to get a property value instead of simply writing
`object[property]` or `object.property` ?
Because this function allows `property` to be a string that can include dot notation
( i.e, 'property.subproperty.subsubproperty' ) .
Note: even if you are getting the value of an array item, here you need to use
dot-notation and not square braces.
Example: if getting the first item of the first item of an array, write:
`getProperty('0.0', array); // instead of array[0][0]`
## Examples
```
let officer = {name: {first: 'Tom', last: 'Arnold'}, rank: 'sergeant'};
let lastName = getProperty('name.last', officer);
// lastName === 'Arnold'
let city = {
name: 'San Francisco',
cityCouncil: {
members: [
{name: {first: 'Megan', last: 'Trainor'}, age: 26},
{name: {first: 'Justin', last: 'Bieber'}, age: 85}
]
}
};
let ageOfMeganTrainor = getProperty('cityCouncil.members.0.age', city);
// ageOfMeganTrainor === 26
```
## Installation
You must have npm installed first. Then, in the command line:
```bash
npm i @writetome51/get-property
```
## Loading
```js
import {getProperty} from '@writetome51/get-property';
```