0

I have this array:

my_json = [
    {
        "DontNeed":"Remove",
        "year":"2015",
        "Some name": "some value"

    },
    {
        "DontNeed":"Remove",
        "year":"2016",
        "Some name": "another value"
    },
];

I want to have that JSON like this:

new_json = [
    {
        "year":"2015",
        "Some name": "some value"

    },
    {
        "year":"2016",
        "Some name": "another value"
    },
];

Is there a way to do this? an known function?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
pmiranda
  • 7,602
  • 14
  • 72
  • 155

4 Answers4

0

You can use the delete operator to remove a known key (in this case property) from an object. Combined with the map function you can quickly remove the property from all entries.

var my_json = [
    {
        "DontNeed":"Remove",
        "year":"2015",
        "Some name": "some value"

    },
    {
        "DontNeed":"Remove",
        "year":"2016",
        "Some name": "another value"
    },
];
var expected = my_json.map(j => {
  delete j['DontNeed']
  return j;
});
console.log(expected);

Edit

As @Mark Meyer mentioned in the comments this will remove the prop/value from my_json as well. If you want to preserve the original see his answer. Just in case that answer goes away for whatever reason: new_json = my_json.map(({DontNeed, ...rest}) => rest);. However, if this answer were to be accept his should be accepted.

Wild Beard
  • 2,919
  • 1
  • 12
  • 24
  • This has the side effect of deleting the property from the original object as well. – Mark Dec 04 '18 at 23:57
0

You've tagged the question with javascript, so I'm assuming that you're looking for a JavaScript solution to this problem.

Presuming that your JSON is in an object of some kind (which I thought was called my_json), you can simply do something like:

for (var x in my_json) {
    delete x['DontNeed'];
}
Martin
  • 16,093
  • 1
  • 29
  • 48
  • I think the issues with using *for..in* with Arrays have been well documented, see [*Why is using “for…in” with array iteration a bad idea?*](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea). You can use *forEach* to do the same thing safely. – RobG Dec 05 '18 at 00:11
  • Hence _you can simply do something like:_ instead of _you must do this:_. However, the point is well made; I wrote the code on the fly and in a hurry. – Martin Dec 05 '18 at 00:13
0

Don't bother having more properties, leave it as it is.

If you really can't, then use Array.map function like this:

my_json = [
    { "DontNeed":"Remove", "year":"2015", "Some name": "some value" },
    { "DontNeed":"Remove", "year":"2016", "Some name": "another value" }
];

new_json = my_json.map(x => ({
    year: x.year,
    'Some name': x['Some name']
}));

console.log(new_json);

Hope it helps.

Washington Guedes
  • 4,254
  • 3
  • 30
  • 56
0

You can destructure off the property (or properties) you don't want and make a new array with map():

let my_json = [
    {"DontNeed":"Remove","year":"2015","Some name": "some value"},
    {"DontNeed":"Remove","year":"2016","Some name": "another value"}
];

let new_json = my_json.map(({DontNeed, ...rest}) => rest)
console.log(new_json)
Mark
  • 90,562
  • 7
  • 108
  • 148