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-getvalue

v1.1.0

Published

Get Object's nested value

Downloads

15

Readme

object-getvalue (npm-link)

Get the Object's property directly including nested properties. It supports the default return value and hasOwnProperty check.

Build Status license Gemnasium

Runtime Error occurs when any key in the sequence is undefined while getting the Object's nested property value.

person.address.location.current.city

To Avoid that, it's best practise to check for each property on object in sequence

if (person.address &&
	person.address.location &&
	person.address.location.current &&
	person.address.location.current.city) {

	// var city = person.address.location.current.city

}

This package helps you in directly getting the nested property value from the Object

getValue(person, 'address.location.current.city') //=> 'Bangalore'

You can use it to avoid ternary operation while fetching the Object value and assigning that to a variable without worrying about any run-time error.

// Traditional way
var city = person.address.location.current.city ? person.address.location.current.city : 'Bangalore';

// Cleaner & Safe way - using getValue()
var city = getValue(person, 'address.location.current.city', 'Bangalore');

Installation

npm install object-getvalue --save

Usage

Arguments

  1. object (Object) The object to query
  2. property (String) Simple or Nested property
  3. (Optional) defaultValue (any) The value returned ONLY if property value resolves to undefined
  4. (Optional) hasOwnProperty (bool - default : false) Checks for the property only on the current object but NOT on Prototype/Parent.

Example

var getValue = require('object-getvalue');
OR
import getValue from 'object-getvalue';

var Person = function() {
	this.name = 'No name defined yet';
	this.age = 25;
	this.address = {
		city: 'Default - New Delhi',
		country: 'Default - India'
	};
};

var Employee = function() {
	this.name = 'Ravi Roshan';
	this.company = 'XYZ Consulting';
	this.empId = 12345;
	this.address = {
		city: 'Bangalore'
	};
	this.details = {
		role: 'Developer',
		designation: 'Consultant',
		domain: 'e-commerce'
	};
	this.friends = [{
		name: 'Siddhant',
		age: 27
	}, {
		name: 'Harsh',
		age: 26
	}, {
		name: 'Argha',
		age: 28
	}];
};

// Inherit the Person Object
Employee.prototype = new Person();

// Create an instance of Employee
var emp1 = new Employee();

// Check for property on Current Object
getValue(emp1, 'name') //=> 'Ravi Roshan'
getValue(emp1, 'details.domain') //=> 'e-commerce'
getValue(emp1, 'details.language.primary') //=> undefined

// Check for property on Prototype Object'
getValue(emp1, 'age') //=> 25 [Getting from prototype Person Object]
getValue(emp1, 'address.area.pincode') //=> undefined

// Check for property on Current Object - with Default Return
getValue(emp1, 'salary', 9999) //=> 9999
getValue(emp1, 'salary', 'Salary not found') //=> 'Salary not found'
getValue(emp1, 'name', 'John') //=> 'Ravi Roshan' [name doesn't resolve to undefined so defaultReturn is not considered]

// Check for property ONLY on Current Object
getValue(emp1, 'age', 'Age not found', true) //=> 'Age not found'

// Check for property on Current OR Prototype Object
getValue(emp1, 'age', 'Age not found', false) //=> 25

// Check for the name property on Friends [array] on Emp [object]
getValue(emp1, 'friends[1].name') //=> 'Harsh'

License

MIT