-2

I have one collection of json objects as follows.

[
  {"FirstName":"Test1","LastName":"User","Email":"test@test.com","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
  {"FirstName":"user","LastName":"user","Email":"u@u.com","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
  {"FirstName":"Ropbert","LastName":"Jones","Email":"Robert@gmail.com","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
  {"FirstName":"hitesh","LastName":"prajapti","Email":"h.prajapati@zzz.com","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
  ]

Now I want to remove some properties (eg. Email & city ) from all this objects without using loop. can somebody guide me How to do this in a better way in javascript?

Manish Sapkal
  • 5,591
  • 8
  • 45
  • 74

2 Answers2

2

You can use delete operator and Array.prototype.forEach like this

objects.forEach(function(currentItem) {
    delete currentItem["Email"];
});
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

You can use a regular expression to remove the properties from the JSON string:

s = s.replace(/,"Email":"(\\"|[^"])*","City":"(\\"[^"])*"/, '');
Guffa
  • 687,336
  • 108
  • 737
  • 1,005