Tengo lo siguiente donde estoy configurando valores dentro de los niños donde los niños son del tipo HTMLCollection .
Todos estos valores que se establecen podrían ser nulos. Si es nulo, deseo eliminar el elemento secundario por completo.
Pero, ¿cómo podría verificar si un niño tiene valor?
Nota: buscando eliminarlo en un bucle porque esas claves ya existen como someToken .
Por lo tanto, una verificación nula para evitar agregarle un valor no será suficiente.
Todavía necesito eliminar la clave/valor (hijo) por completo a través de bucle.
Esencialmente buscando lo que escribiría dentro del control if para ver si un elemento secundario en HTMLCollection tiene un valor.
// 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); } });Yo iría por Object.entries :
for (const [key, value] of Object.entries(myRef.current.children)) { if (value === null) myRef.current.removeChild(child); }