Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda