1

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 ?

Rohit Jindal
  • 11,704
  • 11
  • 56
  • 92
  • How do you decide which properties to send to the server? Do you have an array of property names? – 31piy Jun 10 '18 at 07:19
  • Did you compare if it wouldn't be faster to send the whole object to the server? – baao Jun 10 '18 at 07:21
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Felix Kling Jun 10 '18 at 07:27

4 Answers4

2

There is always the option to create a new object and only pull out the keys you need. See One-liner to take some properties from object in ES 6 for that.


JSON.stringify accepts a "replacer" function as second argument. That allows you to change values during serialization, including filtering them out. Assuming there are fewer properties that you want to keep, you should list those in an array or Set.

var jsonObj = {
  "name": "Rohit",
  "age": 27,
  "city": "Gurgaon"
};
var toKeep = ['age', 'city']; 

console.log(JSON.stringify(
  jsonObj,
  (key, value) => !key || toKeep.indexOf(key) > -1 ? value : undefined,
));
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
0

var jsonObj = {
  "name": "Rohit",
  "age": 27,
  "city": "Gurgaon"
};

var newObj = jsonObj;
var arr = ["name","age"];

for(var i=0;i<arr.length;i++){
  delete newObj[arr[i]]
}

console.log(newObj);

This may solve your problem I feel.

Ullas Hunka
  • 1,939
  • 1
  • 12
  • 26
0

Try the following , It will not modify your original object:

var jsonObj = {
  "name": "Rohit",
  "age": 27,
  "city": "Gurgaon"
};
var storePropeties = ["city"];
var result = Object.keys(jsonObj).reduce(function (obj, key) {
   if(storePropeties.includes(key))
      obj[key] = jsonObj[key];
     return obj
 }, {})

console.log(result);
amrender singh
  • 6,982
  • 3
  • 17
  • 25
0

You should create a new object you want to send to server

My solution:

var jsonObj = {
  "name": "Rohit",
  "age": 27,
  "city": "Gurgaon"
};

// if you don't need specific keys you should delete this and ~keys.indexOf(prop) in condition
var keys = ['name', 'city', 'moscow'];

function newObj(obj, count) {
  var newObj = {};
  var counter = count;

  for (var prop in obj) {
    if (jsonObj.hasOwnProperty(prop) && ~keys.indexOf(prop)) {
      newObj[prop] = obj[prop];
      counter -= 1;
    }

    if(counter === 0) break;
  }

  return newObj;
}

console.log(JSON.stringify(newObj(jsonObj, 20)));
zemil
  • 920
  • 1
  • 7
  • 21