-2

I am having an JSON object looks like this

{
value: {
  "0": {
    dummy: "dummy1",
    data : "One",
  },
},
value2: {
  "0": {
    dummy: "dummy2",
    data : "two",
  },
},
value3: {
  "0": {
    dummy: "dummy3",
    data : "three",
  },
}

}

I want output like this

{
value: {
  "0": {
    data : "One",
  },
},
value2: {
  "0": {
    data : "two",
  },
},
value3: {
  "0": {
    data : "three",
  },
}

}

I am able to traverse the root element of all the objects but not able to traverse the array, tried using map but nothing helped.

Venkat
  • 11
  • 3
  • 1
    There isn't any JSON in your question. [JSON](https://json.org) is a text representation of some data structure (usually an object or an array). What you posted in the question is the data structure (a JavaScript array), not its JSON representation. – axiac May 04 '21 at 13:19

2 Answers2

2

As noted in this answer - https://stackoverflow.com/a/3455416/5867572 - the delete operator allows this.

Seriously - one search found the answer in less than a minute. I have included it because I didn't know about the delete operator and assume others might be interested, but the idea of SO is to answer questions that have had at least a modicum of research.

Also - if I was doing this - I would find a better way than simply repeating the delete operator 3 times - but that needs to be more thought out in the data structure.

Note that

let origData = {
  "value": {
  "1": {
    dummy: "dummy1",
    data : "One"
   }
  },
  "value2": {
    "0": {
      dummy: "dummy2",
      data : "two"
    }
  },
  "value3": {
    "0": {
      dummy: "dummy3",
      data : "three"
    }
  }
}


 delete origData["value"]["1"]["dummy"];
 delete origData["value2"]["0"]["dummy"];
 ddelete origData["value3"]["0"]["dummy"];

 console.log(origData); // logs the original object without the deleted keys / properties
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • Is there a better way to do that dynamically traversing all the objects and removing the keys? Thanks in advance – Venkat May 04 '21 at 14:03
  • yes - but you need to structure the data to support it - look at the data you have - a random collection of keys with variable numbers on them - organise these better then you can simply perform a loop and use the insdex to identify the key you want to delete. But you have to work that out on your own - otherwise you will never learn how to structure your datafor the functionality you are trying to achieve :) – gavgrif May 04 '21 at 14:05
0

Here is an iterative, dynamic solution using object-scan

// const objectScan = require('object-scan');

const data = {
  value: { 0: { dummy: 'dummy1', data: 'One' } },
  value2: { 0: { dummy: 'dummy2', data: 'two' } },
  value3: { 0: { dummy: 'dummy3', data: 'three' } }
};

const prune = objectScan(['*.0.data'], {
  rtn: 'count',
  filterFn: ({ parent, property }) => {
    delete parent[property];
  }
});

console.log(prune(data));
// => 3

console.log(data);
/* => {
  value: { '0': { dummy: 'dummy1' } },
  value2: { '0': { dummy: 'dummy2' } },
  value3: { '0': { dummy: 'dummy3' } }
} */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@14.3.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24