Skip to content

Instantly share code, notes, and snippets.

@joeyred
Last active May 31, 2022 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyred/843550e9522514ddeeb1520a252d32e4 to your computer and use it in GitHub Desktop.
Save joeyred/843550e9522514ddeeb1520a252d32e4 to your computer and use it in GitHub Desktop.
filter object by keys
/**
* Create a new object populated with specifc keys from another object.
* @method specificKeysToNewObject
* @param {Object} object - The original object.
* @param {String[]} keys - The strings to populate the new
* object with.
* @return {Object|Boolean} - The new object with only the passed
* keys in it. If none of the keys
* passed match any of the keys in the
* original object, returns `false`.
*/
export function specificKeysToNewObject(object, keys) {
const output = {};
const objectKeys = Object.keys(object);
let noMatchFound = true;
for (let i = 0; keys.length > i; i++) {
if (findMatch(objectKeys, keys[i])) {
output[keys[i]] = object[keys[i]];
noMatchFound = false;
}
}
if (noMatchFound) {
return false;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment