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
 |  Show 2 more ments

1 Answer 1

Reset to default 9

I 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