-2

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: "samu@kenzie.com",
        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: "patrick@kenzie.com",
      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: "samueleao@kenzie.com",
        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: "danrley@kenzie.com",
      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

Nick Vu
  • 14,512
  • 4
  • 21
  • 31

3 Answers3

1

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:"samu@kenzie.com",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:"patrick@kenzie.com",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:"samueleao@kenzie.com",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:"danrley@kenzie.com",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);
Andy
  • 61,948
  • 13
  • 68
  • 95
0

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

let test = [
    {  
      email: "samu@kenzie.com",
        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: "patrick@kenzie.com",
      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: "samueleao@kenzie.com",
        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: "danrley@kenzie.com",
      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,
}));
Ken Labso
  • 885
  • 9
  • 13
  • You haven't used any destructuring. Your answer also mutates the data which might not be what the OP wants. And if you're going to use this approach `forEach` is a better method - `map` returns an array while `forEach` works in-place. – Andy Dec 15 '22 at 05:04
  • @Andy I have updated my answer and applied destructing. Check it again. – Ken Labso Dec 15 '22 at 05:43
0
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.