looping-tool

1.0.5 • Public • Published

Looping Tool 🚀

This method allows you to get an specific value from an nested object by passing a key as a string.

Installation 🔧

npm install looping-tool

Benefits? 📌

Accessing a specific value from a nested object can be very annoying and you should always test if all the properties you are using exist.

if (OBJECT && OBJECT.whereIsTheKey && OBJECT.whereIsTheKey.here) {
    let value = OBJECT.whereIsTheKey.here.oneOfThis
}

This is necessary to avoid errors like "cannot read property 'here' of undefined". Forget about all that by using this method.

Using the method ⚙️

const loopingTool = require('looping-tool');


let OBJECT = {
  second: {
    test: 'hi',
    test2: 'bie'
  },
  array: ['wii', 'debug', 'hahaha'],
  whereIsTheKey: {
    maybe: 'No',
    here: {
      oneOfThis: ['nop', 'almost', 'here']
    }
  }
};


let findedValue = loopingTool(OBJECT, 'oneOfThis');
console.log("Get Value By Key", findedValue);

result:

Get Value By Key (3) ["nop", "almost", "here"]

License

This package is licensed under the MIT License.
Created by @facundo.marcet.

Package Sidebar

Install

npm i looping-tool

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

3.49 kB

Total Files

4

Last publish

Collaborators

  • facundo.marcet