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

266
Visualizações
How to search and replace key and value of nested object using javascript

I need to search and replace value from a nested object using javascript. I am explaining the object below.

let jsonObj = {
    "service":[
        {
            "name":"restservice",
            "device":"xr-1",
            "interface-port":"0/0/2/3",
            "interface-description":"uBot testing for NSO REST",
            "addr":"10.10.1.3/24",
            "mtu":1024
        }
    ],
    "person": {
    "male": {
      "name": "infinitbility"
    },
    "female": {
      "name": "aguidehub",
      "address": {
           "city": "bbsr",
           "pin": "752109"
      }
    }
  }
}

In the above nested object I need to search the key and replace the value. Here my requirement is in the entire object it will search for the key name and where ever it is found it will replace the value as BBSR. In the above case there are 3 place where name key is present so I need to replace the value accordingly using Javascript.

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

0

You can try the deep search algorithm
Iterate over the entire object
Determine the current property
If the current property is object or array
then continue to recurse the property
If the property is not an object or array
then determine if the object's key == 'name'
if yes, then do what you want to do
If not, then do nothing
like this

const deepSearch = (target) => {
    if (typeof target === 'object') {
        for (let key in target) {
            if (typeof target[key] === 'object') {
                deepSearch(target[key]);
            } else {
                if (key === 'name') {
                    target[key] = 'xxx'
                }
            }
        }
    }
    return target
}
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