destructo
v1.0.1
Published
Destruct js objects and extract what you need!
Downloads
7
Maintainers
Readme
destructo
npm install destructo
yarn add destructo
Basic usage
const destructo = require('destructo')
const target = {
username: 'JohnDoe',
password: 'secret',
id: 100
}
const personDetails = destructo(target, 'username', 'id')
console.log(personDetails) // { username: 'JohnDoe', id: 100 }
Rename
const target = {
someKey: 'someValue',
deep: {
foo: 'deepFoo'
}
}
const newKeyNames = destructo(target, 'someKey:newName', 'deep.foo:newFoo')
console.log(newKeyNames) // {newName: 'someValue', newFoo: 'deepFoo'}
Nested Objects
const target = {
someKey: 'someValue',
deep: {
foo: 'deepFoo',
bar: 'deepBar',
}
}
// If there are deeper objects, continue with dot notation until target is reached.
const deep = destructo(target, 'deep.foo', 'deep.bar')
console.log(deep) // { foo: 'deepFoo', bar: 'deepBar' }
Destructo in action!
const someList = [
{name: 'Lassie', 'animalType': 'dog', color: 'brown'},
{name: 'CatStevens', 'animalType': 'cat', color: 'yellow'},
{name: 'Christian', 'animalType': 'lion', color: 'orange'}
]
const nameAndColor = someList.map(animal => destructo(animal, 'name', 'color'))
console.log(nameAndColor)
/*
[
{name: 'Lassie', color: 'brown'},
{name: 'CatStevens', color: 'yellow'},
{name: 'Christian', color: 'orange'}
]
*/