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

108
Visualizações
Remove child with null values in a loop from a HTMLCollection

I have the following where I am setting values inside children where children is of type HTMLCollection.

All these values being set could be null. If null, I wish to remove the child entirely.

But how could I check if a child has value?

Note: Looking to remove it in a loop cos those keys already exists like someToken.

Thus a null check to stop from adding a value to it will not suffice.
I still need to remove the key/value (child) entirely via loop.

Essentially looking for what I would write inside the if check to see if a child in the HTMLCollection has a value.

// this is coming from another file.
const myRef = React.useRef();

myRef.current.children['someToken'] = null;
myRef.current.children['some id'] = 'someId';
myRef.current.children['another data'] = 'anotherData';

[...myRef.current.children].forEach((child) => {

    // I want to remove the child entirely if there is no value. 
    // for example above, the key someToken, the value is set as null
    // so in this case I want to remove the child. 
    // what would I write in the if check here to check that?

    //  if (!child) will always be false since key exists

    if (!child) {
        myRef.current.removeChild(child);
    }

});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would go for Object.entries:

for (const [key, value] of Object.entries(myRef.current.children)) {
  if (value === null)
    myRef.current.removeChild(child);
}
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