0

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!

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer and the code. I do have a follow up, if I wanted to remove more than one pet rock, can I do so in the same filter expression, or do I need a new separate filter? i.e. d["pet-rock].filter(rock => rock.name !== "Steven" || rock.name !== "Travis"); ?
I would put the "pet rocks slated for removal" into an array and you can modify the filter function as follows: .filter(rock => !["Steven", "Travis"].includes(rock.name))
Yep, that worked! I had also discovered that I was close was well. Should have been "&&" instead of "||". My way also worked: .filter(rock => { return rock.name !== "Steven" && rock.name !== "Travis"}); Thanks for the help again James!
I do like your way better though, looks cleaner!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.