473,406 Members | 2,549 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,406 software developers and data experts.

Creating an array of objects on the fly

Claus Mygind
571 512MB
I can't figure out how to build the array of objects from the string I return from an ajax call.

Let's say I return a string:

{name:"Claus", age:39};;{name:"Tom", age:42};;{name:"John",age:13}

This string is contained in the variable returnData.

I have an array called jsData which I want to be a collection of objects and I would like the result to look like the following:

jsData[0] = {name:"Claus", age:39}
jsData[1] = {name:"Tom", age:42}
jsData[2] = {name:"John",age:13}

What I can't figure out is how to do that. I tried to use :

var jsData = returnData.split(;;)

I also tried to add [ ] to the beginning and end of the string that was returned to returnData, but that did not work either.

I do not wish for "name" and "age" to be constants, they will vary with each ajax query as will the total number of records returned.

I am trying to build a multi-purpose module that will display a table for me which I can use with my varied apps.
Apr 4 '08 #1
7 2724
Claus Mygind
571 512MB
I should have added that when I use the split option each letter in the string becomes an object in the jsData array like this.

jsData[0] = {0:N}
jsData[1] = {1:a}
jsData[2] = {2:m}
jsData[3] = {3:e}
jsData[4] = {:}
jsData[5] = {"}
jsData[6] = {C}
.
.
.
Apr 4 '08 #2
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. var jsData = returnData.split(";;");
Apr 5 '08 #3
Claus Mygind
571 512MB
I am not familiar with "Code: ( javascript )". I have tried to find it on the web with no luck. Could you please post an example of how it is used?
Apr 7 '08 #4
Claus Mygind
571 512MB
Well I finally got it to work. The secret (for me at least) was

1. Proper formatting of the returned string from my ajax call '{<first row>};;{<next row>};;...'

2. Using the "eval( )" method to turn the string into an object in the jsData array.

see my example below.


[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function makeObjectFromString(string) {
eval("var result = " + string);
return result;
}

var returnedString = '{name:"Claus", age:39};;{name:"Tom", age:42};;{name:"John",age:13}';

aString = returnedString.split(';;');

jsData = new Array()

jsData[0] = makeObjectFromString(aString[0]);

for (var key in jsData[0])
{
alert(key);
}

//-->
</SCRIPT>
</HEAD>

<BODY>

</BODY>
</HTML>[/HTML]
Apr 7 '08 #5
acoder
16,027 Expert Mod 8TB
I am not familiar with "Code: ( javascript )". I have tried to find it on the web with no luck. Could you please post an example of how it is used?
Umm... that wasn't part of the code - it was just to show that the following code is JavaScript code.
Apr 7 '08 #6
acoder
16,027 Expert Mod 8TB
Well I finally got it to work. The secret (for me at least) was

1. Proper formatting of the returned string from my ajax call '{<first row>};;{<next row>};;...'

2. Using the "eval( )" method to turn the string into an object in the jsData array.
If you used JSON, one eval would be enough. Anyway, good to see that you've got a working solution.
Apr 7 '08 #7
Claus Mygind
571 512MB
That sounds like something which would speed up processing. I will try to research it as I have not heard of it before and don't know how to implement it.

Thanks for the advice.
Apr 7 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything...
4
by: Faheem Mitha | last post by:
Dear People, I have the current modest goal. Given a user defined class, I want to creata an array of instances of that class. Consider the following class. class cell: def...
8
by: cody | last post by:
i basically want to create an object which contains an array (the last element of the class). the size of the array is determined when the object is created. for performance reasons (avoiding...
5
by: Keiron Waites | last post by:
<script language="JavaScript" type="text/javascript"> <!-- var array1 = new Array(); var array1i = new Array(); array1 = array1i; alert(array1); // --> </script>
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
5
by: jwgoerlich | last post by:
Hello, I need to create a 1 GB Byte array in memory. I can create it on a Win2000 system. On a Win2003 system, the application throws a System.OutOfMemoryException error. Both are running the...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.