JSPM

@writetome51/get-property

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q32923F
  • License MIT

Allows you to retrieve value of object property using dot-notation in a string

Package Exports

  • @writetome51/get-property

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@writetome51/get-property) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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:

npm i @writetome51/get-property

Loading

import {getProperty} from '@writetome51/get-property';