GithubHelp home page GithubHelp logo

deep-obj's Introduction

DeepObject

Set and get values on objects via dot-notation strings.

Example

var DeepObject = require('deep-obj');
var get = DeepObject.get;
var set = DeepObject.set;

var testObject = {
	a: {
		b: {
			c: [{
				ae: 1
			}, {
				ae2: ['demoo']
			}]
		},
		d: [1,2,3]
	},
	'test-abc': {
		a: {
			b: [[1, 2, 3], [4, 5, 6]]
		}
	}
}

console.log(get(testObject, 'a.b.c'), testObject.a.b.c);

console.log(get(testObject, 'a.d[0]'), testObject.a.d[0]);

console.log(get(testObject, 'a.b.c[0]'), testObject.a.b.c[0]);

console.log(get(testObject, 'a.b.c[0]["ae"]'), testObject.a.b.c[0]["ae"]);

console.log(get(testObject, 'a.b.c[0].ae'), testObject.a.b.c[0].ae);

console.log(get(testObject, 'a.b.c[1].ae2[0]'), testObject.a.b.c[1].ae2[0]);

console.log(get(testObject, '["test-abc"].a.b[0][2]'), testObject["test-abc"].a.b[0][2]);

console.log(get(testObject.arr, '[0].a.b'), testObject.arr[0].a.b));

console.log(get(testObject.arr, '[0].a.b.d.e'), undefined));

set(testObject, '["test-abc"].a.b[0][2]', 5);
console.log(get(testObject, '["test-abc"].a.b[0][2]'), 5);

set(testObject, 'a.b.c[0].ae', 10);
console.log(get(testObject, 'a.b.c[0].ae'), 10);

set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);
console.log(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);

API

DeepObject.get(object, path)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The function will return an VALUE or undefined

DeepObject.set(object, path, value)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The function return TRUE or FALSE. Return TRUE if it can set value with the path

DeepObject.parse(path)

Where path is a string like foo.bar or foo.bar[0][1] or [0][1].foo['bar'].

The function will return an array of path;

Ex:

foo.bar => ['foo', 'bar']

foo.bar[0][1] => ['foo', 'bar', 0, 1]

[0][1].foo['bar'] => [0, 1, 'foo', 'bar']

Installation

With npm do:

npm install deep-obj

License

MIT

deep-obj's People

Contributors

0xvoidmain avatar mvn-luanvu-hn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.