Skip to content

Instantly share code, notes, and snippets.

@viniceosm
Last active December 16, 2019 14:17
Show Gist options
  • Save viniceosm/c3e813e672d3fc6f6b2562a4548cc0af to your computer and use it in GitHub Desktop.
Save viniceosm/c3e813e672d3fc6f6b2562a4548cc0af to your computer and use it in GitHub Desktop.
filterKeys ES10
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