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 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.
Pick random property from a Javascript object
get top N values' keys of an Object
Is there any better way to achieve this ?
Share Improve this question asked Jun 10, 2018 at 6:52 Rohìt JíndalRohìt Jíndal 27.3k15 gold badges78 silver badges132 bronze badges 3- How do you decide which properties to send to the server? Do you have an array of property names? – 31piy Commented Jun 10, 2018 at 7:19
- Did you pare if it wouldn't be faster to send the whole object to the server? – baao Commented Jun 10, 2018 at 7:21
- There's no such thing as a "JSON Object" – Felix Kling Commented Jun 10, 2018 at 7:27
4 Answers
Reset to default 3There 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,
));
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)));
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.
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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745162139a4614434.html
评论列表(0条)