GithubHelp home page GithubHelp logo

eliottvincent / dotly Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 129 KB

๐Ÿ”˜ Access properties within an object, using dot-notation.

Home Page: https://www.npmjs.com/package/dotly

License: MIT License

JavaScript 100.00%
dot-notation keyed object

dotly's Introduction

Dotly

Build Status Version Downloads

Access properties within an object, using dot-notation.

Usage

const dotly = require("dotly");

var obj = {
  a: {
    b: {
      c: "hello"
    }
  }
};
console.log(dotly.get(obj, "a.b.c"));
// 'hello'

API

Gets a value at a path within an object

get(object, path, defaultValue) returns the value at the specified path:

  • object must the object from which to get the value
  • path must be a string representing the path, using dot notation (supports wildcard with *)
  • defaultValue can be used as a default value
const { get } = require("dotly");

var obj = {
  a: {
    b: {
      c: "hello"
    },

    d: {
      c: "hello bis"
    }
  }
};

console.log(get(obj, "a"));
// {b: {c: 'hello'}}

console.log(get(obj, "a.b.c"));
// 'hello'

console.log(get(obj, "a.b.c.d"));
// undefined

console.log(get(obj, "a.b.c.d", "hallo"));
// 'hallo'

console.log(get(obj, "a.*.c"));
// [{path: 'a.b.c', value: 'hello'}, {path: 'a.d.c', value: 'hello bis'}]

Sets a value at a path within an object

set(object, path, value) sets a value at the specified path:

  • object must the object in which to set the value
  • path must be a string representing the path, using dot notation
  • value must be the value to set
const { set } = require("dotly");

var obj = {
  a: {
    b: {
      c: "hello"
    }
  }
};

set(obj, "a.b.d.e", "hallo");
console.log(obj);
// {a: {b: {c: 'hello', d: {e: 'hallo'}}}}

set(obj, "a.b", { hello: "hello" });
console.log(obj);
// {a: {b: {hello: 'hello'}}}

Removes a value at a path within an object

remove(object, path, value) removes the value at the specified path:

  • object must the object from which to remove the value
  • path must be a string representing the path, using dot notation
const { remove } = require("dotly");

var obj = {
  a: {
    b: {
      c: "hello"
    }
  }
};

console.log(obj);
// {a: {b: {c: 'hello'}}}

remove(obj, "a.b.c");
console.log(obj);
// {a: {b: {}}}

License

dotly is released under the MIT License. See the bundled LICENSE file for details.

dotly's People

Contributors

eliottvincent avatar

Watchers

 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.