Last active
December 16, 2019 14:17
-
-
Save viniceosm/c3e813e672d3fc6f6b2562a4548cc0af to your computer and use it in GitHub Desktop.
filterKeys ES10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const filterKeys = (_raw, ..._allowed) => { | |
_allowed = _allowed.flat(); | |
_raws = [_raw].flat(); | |
const filtered = _raws.map(_raw => { | |
const filtered = Object.keys(_raw) | |
.filter(key => _allowed.includes(key)) | |
.reduce((obj, key) => { | |
obj[key] = _raw[key]; | |
return obj; | |
}, {}); | |
return filtered; | |
}) | |
return filtered; | |
} | |
const raw = [ | |
{ | |
descricao: 'nu 1', | |
valor: 1000, | |
outracoisa: 'kaaaaaa' | |
}, | |
{ | |
descricao: 'nu 2', | |
valor: 12222, | |
testetop: 'jjjjjjjj' | |
} | |
]; | |
const allowed = ['descricao', 'valor']; | |
console.log(filterKeys(raw, allowed)); | |
/* output: | |
{ | |
descricao: 'nu 1', | |
valor: 1000 | |
}, | |
{ | |
descricao: 'nu 2', | |
valor: 12222 | |
}; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment