Last active
August 14, 2021 16:14
-
-
Save Ambratolm/9b106e6fe584ff6a733bdf8f1c26362a to your computer and use it in GitHub Desktop.
Omits fields of an object in JavaScript.
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
// Function | |
function omit(obj = {}, fields = []) { | |
const rObj = { ...obj }; | |
for (const prop in obj) { | |
if (fields.includes(prop)) { | |
delete rObj[prop]; | |
} | |
} | |
return rObj; | |
} | |
// Test | |
const user = { | |
id: 0, | |
name: "Mohamed", | |
values: [ 1, 2, 3, 4, 5 ] | |
} | |
console.log(omit(user, ["id", "values"])) // Outputs: { name: "Mohamed" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment