sget-js

1.0.2 • Public • Published

sget

Safely access nested JavaScript object properties

About

A utility method to safely access nested JavaScript object properties.

It enables you to write this:

const age = sget(school, ["teachers", 0, "personal", "age"]);

Instead of this:

const age =
  school &&
  school.teachers &&
  school.teachers[0] &&
  school.teachers[0].personal &&
  school.teachers[0].personal.age
    ? school.teachers[0].personal.age
    : null;

Installation

npm install --save sget-js

npm package link

Usage

const menu = {
  id: "file",
  value: "File",
  style: {
    color: "black",
    background: "white"
  }
};

console.log(sget(menu, ["style", "color"])); // "black"
console.log(sget(menu, ["tooltip", "text"])); // undefined instead of access error

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.2
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.2
    0
  • 1.0.1
    0
  • 1.0.0
    0
  • 0.0.0
    0

Package Sidebar

Install

npm i sget-js

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

2.88 kB

Total Files

4

Last publish

Collaborators

  • bjornkrols