object2 - Inspired from Selectn
Get,Put,Delete from an object easily.
npm i object2 --save
npm stats
Overview
Get-allows you to refactor this:
thor && thor.weapon && thor.weapon.stormbreaker
into:
object2.get('weapon.stormbreaker', thor)
Put-allows you to refactor this:
if(!thor.info ) {
thor.info={}
}
if(!thor.info.name){
thor.info.name={}
}
thor.info.name.actorname = "Chris Hemsworth"
into:
object2.put("thor.info.name.actorname",avengers,"Chris Hemsworth")
Delete-allows you to refactor this:
if(thor && thor.weapon && thor.weapon.stormbreaker){
delete thor.weapon.stormbreaker
}
into:
object2.delete('weapon.stormbreaker', thor)
Usage example(s)
get
var object2 = var avengers= thor: info: name: castname:"Thor" actorname:"Chris Hemsworth" weapon: stormbreaker: { console; } object2;Chris Hemsworth object2;Chris Hemsworth object2true;"Yeah!! its activated" object2;undefined
put
var object2 = var avengers = ironman: info: name:"Tony Stark" ** Note: This will change the original object ** object ironman: info: name: actorname:"Robert Downey" object ironman: info: name: actorname:"Robert Downey Jr" object ironman: info: name: actorname:"Robert Downey Jr" castname:"Tony Stark" /*console.log(avengers);=> {ironman:{ info:{ name:{ actorname:"Robert Downey Jr", castname:"Tony Stark" } } }}*/
delete
var object2 = var avengers= thor: info: name: castname:"Thor" actorname:"Chris Hemsworth" weapon: stormbreaker: { console; } /*If the path exists in the given object,then it will delete the key,which is the path, from the object.If the path doesn't exists,then it won't do any thing.*/ ** Note: This will change the original object ** object2; object2; object2; //this won't do anything because of the path doesn't exist in the given object. /*console.log(avengers);=> {thor:{ info:{ name:{ castname:"Thor" } } }}*/