looping-tool
v1.0.5
Published
This package allows you to obtain a value from an nested object by passing a key as a parameter
Downloads
2
Maintainers
Readme
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.