Skip to content

Find the property in your object with Sargent Fields

License

Notifications You must be signed in to change notification settings

arjunshibu/sgt-fields

 
 

Repository files navigation

sgt-fields

NPM

NPM version Build Status Quality Gate NPM monthly downloads NPM total downloads Coverage Status Known Vulnerabilities

Find the property in your object with Sargent Fields

Install

Install with npm:

npm install sgt-fields --save

Usage

var sgtFields = require('sgt-fields');

Get property on object:

var object = {
  a: {
    b: {
      c: 'test'
    }
  }
};
var result = sgtFields.get(object, 'a.b.c');
//=> result = 'test'

Get property on object (supported array):

var object = {
  user: {
    fullName: [{
      name: 'first name'
    }, {
      name: 'second name'
    }]
  }
};
var resultFirstName = sgtFields.get(object, 'user.fullName.0.name');
//=> resultFirstName = 'first name'

var resultSecondName = sgtFields.get(object, 'user.fullName.1.name');
//=> resultSecondName = 'second name'

Set or add property on object:

var object = {
  a: {
  }
};
var result = sgtFields.set(object, 'a.b.c', 'test');
//=> result = { a: { b: { c: 'test' } } }
//=> object = { a: { b: { c: 'test' } } }

Loop through each property on object:

var object = {
  name: 'Fields',
  rank: 'Sgt',
  personalInfo: {
    phone: '987654321',
    email: 'sgt-fields@sgt-fields.com'
  }
};

sgtFields.each(object, (field, value) => {
  console.log(field, value);
});
//=> name Fields
//=> rank Sgt
//=> personalInfo { phone: '987654321', email: 'sgt-fields@sgt-fields.com' }

About

Find the property in your object with Sargent Fields

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%