Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a Javascript array

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
US
I have a string that is in this format: {{Weight:1200,Class:55},{Weight:100,Class:50}}
How can I get this into an array where the dimentions are [n,2].
n can be any number of array elements. 0 to ? whatever.
Or any other ideas, my goal is to have, in some way in javascript, to have the values so I can display them in a table. And right now I am thinking of a 2d array.
Any help or advice or any other way would be very welcomed.

weight class
1200 55
100 50

Thanks,
kss

Ordinary Programmer
 
Hi

I would rewrite the application which generated that string to output JSON instead :
JSON:
[teal][[/teal][teal]{[/teal]Weight[teal]:[/teal][purple]1200[/purple][teal],[/teal]Class[teal]:[/teal][purple]55[/purple][teal]}[/teal][teal],[/teal][teal]{[/teal]Weight[teal]:[/teal][purple]100[/purple][teal],[/teal]Class[teal]:[/teal][purple]50[/purple][teal]}[/teal][teal]][/teal]
Then :
JavaScript:
[gray]// if you have something like this[/gray]
str[teal]=[/teal][green][i]'[{Weight:1200,Class:55},{Weight:100,Class:50}]'[/i][/green]

[gray]// you simply do this[/gray]
[COLOR=darkgoldenrod]eval[/color][teal]([/teal][green][i]'blah='[/i][/green][teal]+[/teal]str[teal])[/teal]

[gray]// then for example this will say : 100[/gray]
[COLOR=darkgoldenrod]alert[/color][teal]([/teal]blah[teal][[/teal][purple]1[/purple][teal]].[/teal]Weight[teal])[/teal]

Feherke.
 
That would be great if you can tell me how to impliment JSON in my page.

This string is being passed to a .js file that ueses document.createElement('table') ect.. to create a table to show these values.

I have inherited this web site, so this is not the way I would have done it, but I have to use what I have.

Thanks,
kss

Ordinary Programmer
 
You could just force the syntax manually. Start with your string: s={{Weight:1200,Class:55},{Weight:100,Class:50}}.
You want to change the first "{" to "[" and the last "}" to "]". The length of the string is: l=s.length. The part of the string you want is from index, 1, to index, l-2. So:
s="["+s.substring(1,l-2)+"]".

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top