GithubHelp home page GithubHelp logo

smoren / nested-accessor-ts Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 39 KB

Accessor for getting and setting values of nested data structures (arrays or objects)

License: MIT License

TypeScript 99.16% JavaScript 0.84%
accessor nested-structures ts typescript

nested-accessor-ts's Introduction

nested-accessor

Accessor for getting and setting values of nested data structures (arrays or objects).

Unit testing

npm i
npm run test

Usage

NestedAccessor

const source = {
  "data": {
    "id": 1,
    "name": "Countries classifier"
  },
  "countries": [{
    "name": "Russia",
    "cities": [{
      "name": "Moscow",
      "extra": {
        "codes": [{
          "value": 7495
        },
          {
            "value": 7499
          }
        ]
      }
    },
      {
        "name": "Petersburg",
        "extra": {
          "codes": [{
            "value": 7812
          }]
        }
      }
    ]
  },
    {
      "name": "Belarus",
      "cities": [{
        "name": "Minsk",
        "extra": {
          "codes": [{
            "value": 375017
          }]
        }
      }]
    }
  ]
};

const accessor = new NestedAccessor(input);

console.log(accessor.get('data.name')); // 'Countries classifier'
console.log(accessor.get('countries.name')); // ['Russia', 'Belarus']
console.log(accessor.get('countries.cities.name')); // ['Moscow', 'Petersburg', 'Minsk']
console.log(accessor.get('countries.cities.extra.codes.value')); // [7495, 7499, 7812, 375017]

accessor.set('data.name', 'New name');
console.log(accessor.get('data.name')); // 'New name'

nested-accessor-ts's People

Contributors

smoren avatar

Stargazers

 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.