JSPM

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

Access(read, write and delete) an object by hierachical keys.

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, write and delete) an object by hierachical keys. And it can be used as an alternative for the null propagation operator

Synopsis

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

If we use a null operator like many other languages and the tc39 proposal

// Use Null Propagation Operator
obj.one?.two                  // 2

// Use object-access
access(obj, 'one.two', 2))    // 2

Usage

// 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.remove(obj, 'one.two.four')        // obj.one.two.four   -> undefined
access.remove(obj, 'one.two.tree')        // obj.one.two        -> {}
access.remove(obj, 'one.two')             // obj.one            -> {}

access(obj, key_list [, default_value])

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

Get value

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

access.set(obj, key_list, 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.

access.remove(obj, key_list)

Removes a key by deleting it if exists.

License

MIT