Home > Back-end >  Removing a property inside an object using destructuring
Removing a property inside an object using destructuring

Time:12-15

My teacher asked me to create a function to remove the property 'password' inside each object using the destructuring method, but I think he was fooling me cause I haven't found a way to do so.

const test = [
    {  
      email: "[email protected]",
        endereco: { rua: "Rua Quitino", bairro: "Nações", zipCode: "78120-000"} ,
      id: 1,
        idade: 26,
      nome: "Samuel Persuhn",
      password: "51686aaasd2",
      stacks: [ "JavaScript", "PostgreSQL", "Node.js" ]
      },
  
    {
        email: "[email protected]",
      endereco: { rua: "Avenida São Paulo", bairro: "Centro", zipCode: "45687-000"},
        id: 2,
      idade: 22,
      nome: "Patrick Nekel",
      password: "supersenha123548",
      stacks: [ "JavaScript", "MongoDB", "Python" ]
      },
    {  
      email: "[email protected]",
        endereco: { rua: "Avenida Brasil", bairro: "Centro", zipCode: "4587-000"},
        id: 3,
      idade: 28,
      nome: "Samuel Leão",
      password: "hash*asdasda7788",
      stacks: [ "HTML5", "CSS3", "React.js" ]
      },
    {
        email: "[email protected]",
      endereco: { rua: "Rua do videomaker", bairro: "Hollywood", zipCode: "44744-000"},
        id: 4,
      idade: 30,
      nome: "Danrley",
      password: "889977",
      stacks: [ "VideoMaker", "Effects", "Roteirista" ]
      }
]

I tried using destructuring inside each object as if each parameter was an array but it didn't work out

CodePudding user response:

The easiest solution is to map over the data to return a new array of objects. For each object destructure the password, and assign all the other object properties to another object using the rest parameter syntax. Then just return that object.

const test=[{email:"[email protected]",endereco:{rua:"Rua Quitino",bairro:"Nações",zipCode:"78120-000"},id:1,idade:26,nome:"Samuel Persuhn",password:"51686aaasd2",stacks:["JavaScript","PostgreSQL","Node.js"]},{email:"[email protected]",endereco:{rua:"Avenida São Paulo",bairro:"Centro",zipCode:"45687-000"},id:2,idade:22,nome:"Patrick Nekel",password:"supersenha123548",stacks:["JavaScript","MongoDB","Python"]},{email:"[email protected]",endereco:{rua:"Avenida Brasil",bairro:"Centro",zipCode:"4587-000"},id:3,idade:28,nome:"Samuel Leão",password:"hash*asdasda7788",stacks:["HTML5","CSS3","React.js"]},{email:"[email protected]",endereco:{rua:"Rua do videomaker",bairro:"Hollywood",zipCode:"44744-000"},id:4,idade:30,nome:"Danrley",password:"889977",stacks:["VideoMaker","Effects","Roteirista"]}];

// `map` over the data and for each object
// destructure out the password, and assign the
// rest of the properties to a new object (I've called
// it `rest` here but you can call it anything. Finally
// return that object
const out = test.map(obj => {
  const { password, ...rest } = obj;
  return rest;
});

console.log(out);

CodePudding user response:

function removeItem(){
    const newTest = test.map(({email, nome, id, stacks, idade, password}, index) => {
        return {email, nome, id, stacks, idade}
    })
    
    return newTest;
}

you wont need to remove or delete as removing any item from list will mutate.

CodePudding user response:

You can loop around the array, destruct the object of each and only get only the properties you need:

let test = [
    {  
      email: "[email protected]",
        endereco: { rua: "Rua Quitino", bairro: "Nações", zipCode: "78120-000"} ,
      id: 1,
        idade: 26,
      nome: "Samuel Persuhn",
      password: "51686aaasd2",
      stacks: [ "JavaScript", "PostgreSQL", "Node.js" ]
      },
  
    {
        email: "[email protected]",
      endereco: { rua: "Avenida São Paulo", bairro: "Centro", zipCode: "45687-000"},
        id: 2,
      idade: 22,
      nome: "Patrick Nekel",
      password: "supersenha123548",
      stacks: [ "JavaScript", "MongoDB", "Python" ]
      },
    {  
      email: "[email protected]",
        endereco: { rua: "Avenida Brasil", bairro: "Centro", zipCode: "4587-000"},
        id: 3,
      idade: 28,
      nome: "Samuel Leão",
      password: "hash*asdasda7788",
      stacks: [ "HTML5", "CSS3", "React.js" ]
      },
    {
        email: "[email protected]",
      endereco: { rua: "Rua do videomaker", bairro: "Hollywood", zipCode: "44744-000"},
        id: 4,
      idade: 30,
      nome: "Danrley",
      password: "889977",
      stacks: [ "VideoMaker", "Effects", "Roteirista" ]
      }
];

test = test.map(({ email, id, idade, nome, stacks }) => ({
  email, id, idade, nome, stacks,
}));
  • Related