admin管理员组文章数量:1402959
I wish to iterate over an object's properties and change them all to include ""
around the value stored in them.
This object is passed to a REST call and the above format must be enforced. I prefer to handle the addition of ""
in a central location, rather when assigning the actual values (the code is very plex and long).
I know that you can iterate through the object's properties easily:
$.each(queryOptions, function(obj){console.log(obj)})
However, can I somehow get reference to the actual property and set it from within the iteration?
Input:
queryOptions.value1 = 1234;
queryOptions.value2 = "testing";
queryOptions.value3 = 555;
Desired output:
queryOptions.value1 = "1234";
queryOptions.value2 = ""testing"";
queryOptions.value3 = "555";
Thanks
I wish to iterate over an object's properties and change them all to include ""
around the value stored in them.
This object is passed to a REST call and the above format must be enforced. I prefer to handle the addition of ""
in a central location, rather when assigning the actual values (the code is very plex and long).
I know that you can iterate through the object's properties easily:
$.each(queryOptions, function(obj){console.log(obj)})
However, can I somehow get reference to the actual property and set it from within the iteration?
Input:
queryOptions.value1 = 1234;
queryOptions.value2 = "testing";
queryOptions.value3 = 555;
Desired output:
queryOptions.value1 = "1234";
queryOptions.value2 = ""testing"";
queryOptions.value3 = "555";
Thanks
Share Improve this question edited May 25, 2012 at 14:23 itayw asked May 25, 2012 at 14:07 itaywitayw 6149 silver badges20 bronze badges 7- 2 Are you pletely sure that you really need to do that? It seems pretty odd to me. – Pointy Commented May 25, 2012 at 14:09
- Are you sure that you don't actually have to serialize the object as JSON? That would make some sense, but having to add quotes inside strings is weird. – Pointy Commented May 25, 2012 at 14:12
- @Pointy, this is a very specific and weird implementation caused by .NET even weirder handling of json with its .NET webservices support for json. They have improved their support with WCF and Web API which I intend to migrate to in the future. – itayw Commented May 25, 2012 at 14:14
-
2
queryOptions.value2 = testing;
- What's that supposed to mean? – Eric Commented May 25, 2012 at 14:14 - testing is a string, when you view the object's properties it will be surrounded by "" in the viewer. But, in order to demonstrate my point I removed the "". – itayw Commented May 25, 2012 at 14:16
1 Answer
Reset to default 9I agree with Pointy that this seems an odd requirement. But if it's really a requirement:
Using $.each
:
$.each(queryOptions, function(key) {
queryOptions[key] = '"' + queryOptions[key] + '"';
});
Or just using JavaScript without any library stuff:
var key;
for (key in queryOptions) {
if (queryOptions.hasOwnProperty(key)) {
queryOptions[key] = '"' + queryOptions[key] + '"';
}
}
本文标签: jqueryJavascriptIterate the properties of an object and change themStack Overflow
版权声明:本文标题:jquery - Javascript - Iterate the properties of an object and change them - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744093193a2589804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论