Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jrf-path-exists

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jrf-path-exists

Does the object have a path

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Does the object have a path

npm i jrf-path-exists

Get the value of an object along the path.

If the path does not exist, then undefined or the default value will be returned.

/**
  *  Get the value of an object along the path.
  *  @param {object} obj - object
  *  @param {string} path - path
  *  @param {*} [defaultValue] - default value
  */
pathExists(obj, path, defaultValue);
const user = response && response.data && response.data.nodes && response.data.nodes[0] && response.data.nodes[0].builds && response.data.nodes[0].builds[0] && response.data.nodes[0].builds[0].user;
const user = pathExists(response, 'response.data.nodes[0].builds[0].user');

let user = response && response.data && response.data.nodes && response.data.nodes[0] && response.data.nodes[0].builds && response.data.nodes[0].builds[0] && response.data.nodes[0].builds[0].user;
user = user || {username: ''};
const user = pathExists(response, 'response.data.nodes[0].builds[0].user', {username: ''});
const pathExists = require('jrf-path-exists');

const obj = {
  a: {
    b: {
      c: 'hello world'
    }
  },
  d: {
    arr: ['hello', 'world', {a: 8, b: ['one', 'two', 'three']}, ['q', 'w', 'r', ['h', 'l', 'o']]]
  },
  e: ['one el', 'two el']
};

const b = 'b';

console.log(pathExists(obj, 'a.b.c', null));     // --> 'hello world'
console.log(pathExists(obj, `a[${b}].c`));       // --> 'hello world'
console.log(pathExists(obj, `a.${b}.c`));        // --> 'hello world'
console.log(pathExists(obj, 'a.b.c.d.s'));       // --> undefined
console.log(pathExists(obj, 'd.arr[2].b[1]'));   // --> 'two'
console.log(pathExists(obj, 'd.arr[3].3.2'));    // --> 'o'
console.log(pathExists(obj, 'd.arr[3][3][2]'));  // --> 'o'
console.log(pathExists(obj, 'e[1]'));            // --> 'two el'
console.log(pathExists(obj, 'e[4]'));            // --> undefined
console.log(pathExists(obj, 'e[4]', null));      // --> null
console.log(pathExists(obj, 'e[4]', false));     // --> false
console.log(pathExists(obj, 'e[4]', 0));         // --> 0
console.log(pathExists(obj, 'e[4]', []));        // --> []
console.log(pathExists(obj, 'e[4]', ''));        // --> ''
console.log(pathExists(obj, 'e[4]', {}));        // --> {}

Keywords

FAQs

Package last updated on 26 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc