32

I have this JSON object below;

[
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
]

I want to remove the YYY key-value from the json object such that the new json object will look like this;

[
    {
        XXX: "2",       
        ZZZ: "4"
    },
    {
        XXX: "5",       
        ZZZ: "7"
    },
    {
        XXX: "1",       
        ZZZ: "3"
    }
]

I tried delete jsonObject['YYY'] but this is not correct. How can this be done in javascript? Thank you.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • This question is more about handling arrays of objects than anything to do with JSON.. once you have a Javascript object that you are manipulating in memory, the fact that it started life as JSON doesn't really enter into it. – Mark Reed Jul 16 '14 at 01:58
  • possible duplicate of [Remove element from JSON Object](http://stackoverflow.com/questions/15451290/remove-element-from-json-object) – Code Lღver Apr 28 '15 at 07:24

7 Answers7

63

What you call your "JSON Object" is really a JSON Array of Objects. You have to iterate over each and delete each member individually:

for(var i = 0; i < jsonArr.length; i++) {
    delete jsonArr[i]['YYY'];
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
15

Another solution that avoids mutating the original array and uses a more modern features (object rest operator & destructuring)

const arr = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
]

// destructure 'YYY' and return the other props only
const newArray = arr.map(({YYY, ...rest}) => rest)

console.log(newArray)
gafi
  • 12,113
  • 2
  • 30
  • 32
9

Iterate over the values in the array, and remove the value with key YYY:

var obj = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

for (var i = 0; i < obj.length; i++) {
    delete obj[i].YYY
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
3

Because your JSON is inside of an array, you need to access each element in the array and delete their properties "YYY" Or use map to create a reduced object, like so:

var objs = [
    {
        XXX: "2",
        YYY: "3",
        ZZZ: "4"
    },
    {
        XXX: "5",
        YYY: "6",
        ZZZ: "7"
    },
    {
        XXX: "1",
        YYY: "2",
        ZZZ: "3"
    }
];

objs = objs.map(function(obj) {
    return { XXX: obj.XXX, ZZZ: obj.ZZZ };
});

Using the map function. MDN: Array.prototype.map()

Shelby L Morris
  • 726
  • 5
  • 10
  • Your answer is exactly the same as Justin's – William Barbosa Jul 16 '14 at 01:45
  • You're not even using map properly. Since you're not creating a new array, the best practice would be to use forEach instead of map. Also, your answer really doesn't add anything to the question. – William Barbosa Jul 16 '14 at 01:49
  • might as well use forEach if you don't need a return; it doesn't matter much to the machine, but humans might find it more readable and indicative of intent. – dandavis Jul 16 '14 at 01:50
  • to each his own. there are plenty of duplicate answers here as they all were posted at roughly the same down. relax. – Shelby L Morris Jul 16 '14 at 01:51
  • Correct answer. In fact, it is very original and different from the rest. Thanks a lot. But I can only mark one answer as the correct answer on StackOverflow. Sorry. – guagay_wk Jul 16 '14 at 02:07
  • no problem, appreciate it. glad you found a solution. – Shelby L Morris Jul 16 '14 at 02:40
1

jsonObject is an array, so you need to reference the array elements. Try delete jsonObject[0].YYY

Jason Baker
  • 2,471
  • 3
  • 23
  • 28
1

JSON is text, so if you really do have text like:

var s = '[{XXX:"2",YYY:"3",ZZZ:"4"},{XXX:"5",YYY:"6",ZZZ:"7"},{XXX:"1",YYY:"2",ZZZ:"3"}]';

then you can remove the YYYs using string replace, something like:

s.replace(/YYY[^,}]+,/g,''); // [{XXX:"2",ZZZ:"4"},{XXX:"5",ZZZ:"7"},{XXX:"1",ZZZ:"3"}]

You may need to account for YYY at the start or end of the string too. If you have an object that is created by an object literal, then use one of the other answers.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Interesting approach. Even if it were a text string I would tend to opt for converting to the object and then filtering it with JS rather than regex. It's worth noting that valid JSON requires keys to be quoted as well. So it would be `'[{"XXX":"2", "YYY": "3", "ZZZ": "4"}, ...]';`, and you would have to account for the quotation marks as well. – nbrooks Jul 16 '14 at 02:12
1

You can use the map method of javascript's array.

var array = [{
    XXX: "2",
    YYY: "3",
    ZZZ: "4"
},{
    XXX: "5",
    YYY: "6",
    ZZZ: "7"
},{
    XXX: "1",
    YYY: "2",
    ZZZ: "3"
}];
var new_array = array.map(function(obj){
    return {'XXX':obj.XXX,'ZZZ':obj.ZZZ};
});

The desired array is stored in new_array variable.