Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Delete Objects in JavaScript. I'm a bit confused.I have a problem with removeName(person)

I'm a bit confused with JavaScript's delete operator.I am begginer in JS and I have a problem with removeName(person). Take the following piece of code:

let user = {};
user.name = "name";


export default function removeName (person){

  delete user.name;
  return new Object(person)


}

removeName(user);
console.log(user);

After this piece of code has been executed,I take as output {} but I want the below the function

removeName (person), accept the person object as a parameter, and modifies the person object by deleting the property name field. THE function will not return anything, it will modify the object directly.

I'm a bit confused because I think that i solve but I do not get the result I need.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are two big differences between the expectations you have described and your code:

  • you remove a property from user, which is the same object that you pass in this specific case, but if you passed something else, your current function would incorrectly remove user's name even if you intended to remove the name of another object
  • your function returns a value, while you stated that it's not your intention

let user = {};
user.name = "name";


function removeName (person){

  delete person.name;

}

removeName(user);
console.log(user);

about 4 years ago · Juan Pablo Isaza Relatório

0

The user and the person are the same object in the heap(they have the same reference/pointer). So if you would like to remove property only for person object in function you need to create a copy of this object and remove there. If you need a function not to return anything - just removing the person property - try this:

let user = {};
user.name = "name";


export default function removeName (person){
  let copy = Object.assign({}, person);
  delete copy.name;

}

removeName(user);
console.log(user);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda