Get unlimited public & private packages + team-based management with npm Teams.Learn more »

hasproperty

0.0.2 • Public • Published

Multilevel Object.hasOwnProperty

Installation

npm install --save hasproperty

Usage

var hasProperty = require('hasproperty');
 
var person = {
  name: 'John Doe',
  age: 32,
  spouse: {
    name: 'Jane',
    age: 'she lied about her age'
  },
  dogs: [
    {
      name: 'Rex',
      breed: 'Rottweiler'
    },
    {
      name: 'Kiddo',
      breed: 'German Dog',
      pedigree: true
    }
  ]
};
 
console.log(hasProperty('name', person)); // true
 
console.log(hasProperty('spouse.name', person)); // true
 
console.log(hasProperty('dogs', person)); // true
 
console.log(hasProperty('dogs.0.name', person)); // true
 
console.log(hasProperty('dogs.0.pedigree', person)); // false
console.log(hasProperty('dogs.1.pedigree', person)); // true
 

Adding hasproperty to Object's prototype

Object.prototype.recursiveHasOwnProperty = function (prop) {
  return require('hasproperty')(prop, this);
};
 
var person = {
  dog: {
    name: 'Rex'
  }
};
 
console.log(person.recursiveHasOwnProperty('dog.name')); // true

TODO

Write some tests.

Install

npm i hasproperty

DownloadsWeekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • avatar