0

I have JSON object...and want to delete name key value i.e(name-11,name-22) from every child till last child...using java script function...

{
  "id": "1",
  "name": "11",
  "child": [
{
  "id": 2,
  "name": "22",
  "child": [
    {
      "id": 3,
      "name": "33",
      "child": [
        {
          "id": 4,
          "name": "44",
          "child": [
            {
              "id": 5,
              "name": "55",
              "child": [
                {
                  "id": 6,
                  "name": "66"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
 ]
}

Is any right way to do this??

Avinash
  • 137
  • 1
  • 10
  • Yes, there is a right way, It is called recursive traversal. Please post your efforts towards solving this problem, and essentially, create a [mcve]. – 31piy Aug 31 '18 at 14:37
  • no, it looks like an object, not a [JSON](https://json.org/). what exacly want you to do? – Nina Scholz Aug 31 '18 at 14:38

2 Answers2

1

May be something like this...

let myJson = {
  "id": "1",
  "name": "11",
  "child": [{
    "id": 2,
    "name": "22",
    "child": [{
      "id": 3,
      "name": "33",
      "child": [{
        "id": 4,
        "name": "44",
        "child": [{
          "id": 5,
          "name": "55",
          "child": [{
            "id": 6,
            "name": "66"
          }]
        }]
      }]
    }]
  }]
};
let currentObject = myJson;
while (currentObject.child && currentObject.child[0]) {
  delete currentObject.name;
  currentObject = currentObject.child[0];
}
console.log(myJson)
Devansh J
  • 4,006
  • 11
  • 23
0

javascript has a delete operator it works like this:

delete object.name

now you can get this together with a for loop and delete every entry

Ricardo Costa
  • 704
  • 6
  • 27