JSPM

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

Access(read and write) an object hierachically.

Package Exports

  • object-access

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 (object-access) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status

object-access

Access(read and write) an object hierachically.

Install

$ npm install object-access --save

Usage

var access = require('object-access');
var obj = {
  one: {
    two: 2
  }
};

// Get
access(obj, 'one.two');                   // 2
access(obj, ['one', 'two']);              // 2
access(obj, ['one', 'three']);            // undefined
access(obj, ['one', 'three', 'four']);    // undefined
access(obj, ['one', 'three', 'four'], 4); // 4

// Set
access.set(obj, 'one.two', 3);            // obj.one.two.three  -> 3
access.set(obj, ['one', 'two'], 5);       // obj.one.two.three  -> 4

// If the subtle object is not found, it will create one
access.set(obj, ['three', 'four'], 6);    // obj.three.four     -> 6
access.set(obj, 'one.two.tree', 3);       // obj.one.two        -> 3
                                          // `obj.one.two` exists, and is not an object, then skip

// Force setting
access.set(obj, 'one.two.tree', 3, true); // obj.one.two        -> {three: 3}

access(obj, keys [, default_value])

access.get(obj, keys [, default_value])

Get value

  • obj Object
  • keys Array|string see the example above
  • default_value any= if keys not found, then returns the default_value, if default_value is not specified, undefined will be returned.

access.set(obj, keys, value [, force])

If the property already exists and is not an object, access.set() will do nothing by default.

Use force=true to force setting the value, and the old property will be overidden.

License

MIT