Last active
May 31, 2022 04:10
-
-
Save joeyred/843550e9522514ddeeb1520a252d32e4 to your computer and use it in GitHub Desktop.
filter object by keys
This file contains hidden or 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
/** | |
* 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