admin管理员组文章数量:1415460
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);
本文标签: JavaScriptExtract properties from JSON objectStack Overflow
版权声明:本文标题:JavaScript : Extract properties from JSON object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745162138a2645484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论