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

63
Visualizações
How to reset the value of a nested object?

Below the given object i need to reset the value for the key "label", "value", "ward" and "tole" to empty(' ') using javascript.

state ={
         "label": "लुम्बिनी प्रदेश",
         "value": 21,

         "province": {
            "label": "लुम्बिनी प्रदेश",
            "value": "23"
         },
         "district": {
            "label": "अर्घाखाँची",
            "value": "20"
         },
         "local_level": {
            "label": "भूमिकास्थान नगरपालिका",
            "value": "17"
         },
         "ward": "10",
         "tole": "venas"
       }

I have tried to get the keys of the object

Object.keys(state).map(item=>({key: item}))

but don't have idea how to reset the value of the object.

The final result should look like this. the state object may evolve i.e the key and value of the object may be added or may be removed.

state ={
         "label": '',
         "value": '',

         "province": {
            "label": '',
            "value": ''
         },
         "district": {
            "label": '',
            "value": ''
         },
         "local_level": {
            "label": '',
            "value": ''
         },
         "ward": '',
         "tole": ''
       }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can iterate through the object using the for...in Loop in JS. And then, if you find those keys, then just replace their values with an empty string. And since, you also want to check again, that if the value of the keys are objects, and if they are, then you can again loop through them, and change their value to an empty string.

for (let key in state) {
  if (typeof state[key] === 'object') {
    
    // if the value of the key is an object, then we will again loop through the value of that key, and change the value of those keys to an empty string.

    for (let nestedKey in state[key]) {
      // console.log(nestedKey, state[key][nestedKey]);
      
      if (
        nestedKey === "label" ||
        nestedKey === "value" ||
        nestedKey === "ward" ||
        nestedKey === "tole"
      ) {
        state[key][nestedKey] = " ";
      }
    }
  }

  if (key === "label" || key === "value" || key === "ward" || key === "tole") {
    state[key] = " ";
  }
}

This code will work, and will change all those values to empty string.

about 4 years ago · Juan Pablo Isaza Relatório

0

let setToDefault = state => Object.assign(state, { label: '', value: '', ward: '', tole: '' });
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