Created
October 31, 2023 01:45
-
-
Save luizpicolo/0573e8737d3e827be6d720a66241c8dd to your computer and use it in GitHub Desktop.
This file contains 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
class JSONManipulator { | |
constructor(jsonObject) { | |
this.jsonObject = jsonObject; | |
} | |
removeAttributes(attributesToRemove) { | |
if (Array.isArray(attributesToRemove) && attributesToRemove.length > 0) { | |
for (const attribute of attributesToRemove) { | |
if (this.jsonObject.hasOwnProperty(attribute)) { | |
delete this.jsonObject[attribute]; | |
} | |
} | |
} else { | |
console.log("O vetor de atributos a serem removidos está vazio ou não é um array."); | |
} | |
} | |
} | |
// Exemplo de uso: | |
const inputJSON = { | |
"nome": "João", | |
"idade": 30, | |
"cidade": "São Paulo", | |
"profissao": "Engenheiro" | |
}; | |
const manipulator = new JSONManipulator(inputJSON); | |
manipulator.removeAttributes(["idade", "profissao"]); | |
console.log(manipulator.jsonObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment