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

125
Vistas
How to remove a child object from a JSON object using javascript

I'm trying to search through a JSON object to find the pet-rock that I want to delete. Here is a simple JSON that I'm working on:

myData.json:

{
    "data": [
        {
            "name": "John",
            "age": "25",
            "pet-rocks": [
                {
                    "name": "Travis",
                    "age": "9"
                },
                {
                    "name": "Steven",
                    "age": "5"
                },
                {
                    "name": "Oliver",
                    "age": "7"
                }
            ]
        },
        {
            "name": "Jane",
            "age": "25",
            "pet-rocks": [
                {
                    "name": "Jesse",
                    "age": "4"
                },
                {
                    "name": "Carol",
                    "age": "8"
                },
                {
                    "name": "Jake",
                    "age": "7"
                }
            ]
        }
    ]
}

I would like to do a search for "Steven" and remove that pet-rock from the list. Here are the things I've tried:

MyJSFile.js:

const myDataObject = require('./myData.json');

for (let key in myDataObject) {
    let value = myDataObject[key];
    if (value === "Steven")
        {
            delete myDataObject[key];
        }
    //To see if I get the data I want
    console.log(key, value);
}

However, my output is strange and I'm not sure how to get to that child node of petrock. It appears that the petrocks are in object form, and I'm not sure how to get to them. here is the ouput of my console below. I assume it didn't get delete as there are still 3 petrock objects in the data.

data [
  {
    name: 'Robert',
    age: '25',
    'pet-rocks': [ [Object], [Object], [Object] ]
  },
  {
    name: 'Robert',
    age: '25',
    'pet-rocks': [ [Object], [Object], [Object] ]
  }
]

Any help or suggestions would be greatly appreciated! Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The pet rock named Steven is not a direct child of myDataObject, so you can't delete it like that. You can loop through the "data" array, rebuilding the "pet-rocks" array for each element. A simple filter to remove any pet rocks named Steven should work.

const myDataObject = {
    "data": [
        {
            "name": "John",
            "age": "25",
            "pet-rocks": [
                {
                    "name": "Travis",
                    "age": "9"
                },
                {
                    "name": "Steven",
                    "age": "5"
                },
                {
                    "name": "Oliver",
                    "age": "7"
                }
            ]
        },
        {
            "name": "Jane",
            "age": "25",
            "pet-rocks": [
                {
                    "name": "Jesse",
                    "age": "4"
                },
                {
                    "name": "Carol",
                    "age": "8"
                },
                {
                    "name": "Jake",
                    "age": "7"
                }
            ]
        }
    ]
};

myDataObject.data.forEach(d => {
  d["pet-rocks"] = d["pet-rocks"].filter(rock => rock.name !== "Steven");
});

console.log(myDataObject);

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