Problem statement : I am having a JSON object contains n
number of properties inside it. I need to pass some properties from this JSON object as a JSON string to the server.
Tried : I used Object.defineProperty() method to make the enumerable
as false
of the object properties that I don't want to pass in my JSON string
. Please find below the code snippet to get more clear picture.
var jsonObj = {
"name": "Rohit",
"age": 27,
"city": "Gurgaon"
};
Object.defineProperty(jsonObj, 'name', {
enumerable: false
});
console.log(JSON.stringify(jsonObj));
Problem facing with above code :
Suppose i have a 100
number of properties in an object and i want to pass only 20
properties to the server out of 100
. Then i have to write below line of code 80
times to remove the unwanted properties and it will affect the performance of the application.
Object.defineProperty(jsonObj, 'name', { enumerable: false });
I also looked into already asked questions on SO but did not find any suitable answer for this problem statement.
Is there any better way to achieve this ?