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 ?

3
  • How do you decide which properties to send to the server? Do you have an array of property names? Commented Jun 10, 2018 at 7:19
  • Did you compare if it wouldn't be faster to send the whole object to the server? Commented Jun 10, 2018 at 7:21
  • There's no such thing as a "JSON Object" Commented Jun 10, 2018 at 7:27

4 Answers 4

3

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,
));

Sign up to request clarification or add additional context in comments.

Comments

1

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)));

4 Comments

for...in can iterate over properties in arbitrary order, so this would pick 20 random properties?
@FelixKling, I dont understand your question
I guess I should turn it into a statement: this solution will select 20 arbitrary properties, which doesn’t seem to be what the OP wants.
@FelixKling, And now? I changed the code :) do you like?
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.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.