Skip to content

Instantly share code, notes, and snippets.

@luizpicolo
Created October 31, 2023 01:45
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 luizpicolo/0573e8737d3e827be6d720a66241c8dd to your computer and use it in GitHub Desktop.
Save luizpicolo/0573e8737d3e827be6d720a66241c8dd to your computer and use it in GitHub Desktop.
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