Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!