473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Convert string to associative array

Hi All,

I have one small doubt regarding associative arrays. Request somebody
to clarify.

Is there a javascript utility function (like split) to convert a
string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"

Thank you,
Venkatesh

Feb 16 '07 #1
2 25869
On Feb 16, 4:57 pm, "Venkatesh" <venkatbab...@gmail.comwrote:
Hi All,

I have one small doubt regarding associative arrays. Request somebody
to clarify.

Is there a javascript utility function (like split) to convert a
string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"
Javascript doesn't have associative arrays - it has objects with
properties and values, which gives them some features similar to
associative arrays.

What you seem to have is a JSON string, so you can use eval:

var obj = eval("{'MSRP':20000,'cashDown':2000,'tradeIn':1000 }");

--
Rob

Feb 16 '07 #2
>Is there a javascript utility function (like split) to convert a
>string to associative array? My string is having elements in json
notation - in the form "{'MSRP': 20000, 'cashDown': 2000, 'tradeIn':
1000}"

Javascript doesn't have associative arrays - it has objects with
properties and values, which gives them some features similar to
associative arrays.

What you seem to have is a JSON string, so you can use eval:

var obj = eval("{'MSRP':20000,'cashDown':2000,'tradeIn':1000 }");
There is a syntactic ambiguity in JavaScript that will cause that to fail.
Failure can be avoided by wrapping the string in parens, so that it will be
interpreted as an expression rather than a block.

var obj = eval('(' +
"{'MSRP': 20000,'cashDown': 2000,'tradeIn': 1000}" + ')');

http://www.JSON.org/
Feb 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
1
by: Robert Oschler | last post by:
With PHP 4, is there a way to convert an associative array to an object? For example, suppose I have the following arrays inside a nested associative array ($nestedAA): $AA1 = 'fieldValue1'; ...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: MBS | last post by:
Greetings. I'm still pretty new to PHP and I have a question. I know that variables are preceeded by a "$" (dollar sign). Typically, a variable has one value, unless it is an array. Then it is...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.