npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-array-path-exists

v3.0.0

Published

Determine if object or array path is valid.

Downloads

10

Readme

object-array-path-exists

Determines if the path of an object / array is valid.
This library supports arrays and objects that are nested. Returns true if path exists. Returns false if path does not exist.

Installing

npm install object-array-path-exists
yarn add object-array-path-exists

Initializing

import pathExists from "object-array-path-exists";
var pathExists = require("object-array-path-exists");

Examples

Example set 1 - BASIC

//basic nested object
const obj1 = {
  country: {
    province: {
      city: "Toronto"
    }
  }
};

//valid
pathExists(obj1, "country"); //true
pathExists(obj1, "country.province.city"); //true

//invalid
pathExists(obj1, "country.pikachu"); //false
pathExists(obj1, "country.province.invalidKey"); //false;
//basic nested object
const obj2 = {
  element: {
    batallion: {
      unit: {
        squad: {
          team: "Alpha"
        }
      }
    }
  }
};

//valid
pathExists(obj2, "element"); //true
pathExists(obj2, "element.batallion.unit.squad.team"); //true

//invalid
pathExists(obj2, "element1"); //false
pathExists(obj2, "element.batallion.unit.squad.team1"); //false
//basic array
const arr1 = [1, 2, 3];

//valid
pathExists(arr1, "0"); //true
pathExists(arr1, "2"); //true

//invalid
pathExists(arr1, "3"); //false
pathExists(arr2, "0.0"); //false
//basic nested array
const arr2 = [1, ["2a", "2b"], 3, ["4a", "4b"]];

//valid
pathExists(arr2, "1.1"); //true
pathExists(arr2, "3.0"); //true

//invalid
pathExists(arr2, "0.0"); //false
pathExists(arr2, "3.2.1.0"); //false

Example set 2 - INTERMEDIATE

//nested object - non standard key (bracket notation)
const obj3 = {
  "123-country": {
    "123-province": {
      "123-city": "Toronto"
    }
  }
};

//valid
pathExists(obj3, "123-country"); //true
pathExists(obj3, "123-country.123-province.123-city"); //true

//invalid (Note: incorrect path formats)
pathExists(obj1, 123); //false
pathExists(obj1); //false
pathExists(obj1, []); //false
//nested object - non standard key (bracket notation)
const obj4 = {
  "123-element": {
    "123-batallion": {
      "123-unit": {
        "123-squad": {
          "123-team": "Alpha"
        }
      }
    }
  }
};

//valid paths
pathExists(obj4, "123-element"); //true
pathExists(obj4, "123-element.123-batallion.123-unit.123-squad.123-team"); //true

//invalid (Note: incorrect object/array format)
pathExists("", "country.province.city"); //false
pathExists(null, "country.province.city"); //false
pathExists(undefined, "country.province.city"); //false
pathExists(false, "country.province.city"); //false

Example set 3 - ADVANCED

//object and array combination
const arrObj1 = [{ id: 0, name: "name 0" }, { id: 1, name: "name 1" }];

//valid
pathExists(arrObj1, "0.id"); //true
pathExists(arrObj1, "1.name"); //true

//invalid
pathExists(arrObj1, "0.id.x"); //false
pathExists(arrObj1, "0.2.x"); //false
//object and array combination
const arrObj2 = {
  series: "pokemon",
  name: {
    firstName: "pika",
    lastName: "chu"
  },
  type: "electric",
  species: "mouse",
  movies: [
    "movie 0",
    ["movie 1a", "movie 1b", "movie 1c"],
    "movie 2",
    "movie 3",
    "movie 4",
    "movie 5"
  ]
};

//valid
pathExists(arrObj2, "movies.5"); //true
pathExists(arrObj2, "movies.1.2"); //true

//invalid
pathExists(arrObj2, "name.middleName"); //false
pathExists(arrObj2, "movies.2.0"); //false