I'm working with a JavaScript Array and I'm looking to filter it to only include certain keys:
function cleanArray(arr, whitelist) {
// remove all non-whitelisted keys from the array
}
let origArr = [
{ keep1: 'abc', keep2: 'def', buh1: false, buh2: false },
{ keep3: 'abc', keep4: 'def', buh3: false, buh4: true },
{ keep5: 'abc', keep6: 'def', buh5: false, buh5: false }
];
let whiteList = ['keep1', 'keep2', 'keep3', 'keep4', 'keep5'];
let resultArr = cleanArray(origArr, whiteList);
// expected output
resultArr = [
{ keep1: 'abc', keep2: 'def' },
{ keep3: 'abc', keep4: 'def' },
{ keep5: 'abc', keep6: 'def' }
];
How can I go about removing all non-white-listed keys from this Array without simply iterating through it? It doesn't have to be immutable; readability is key here.
Thanks for your suggestions!