473,503 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String to Data Object

Hi, I have a delimited string that I would like to load into a data
object. Here is what I have so far:

//single string of data; each record is delimited by "%"
//and each value within each record is delimited by ";"
var myString = "foo1;001;bar1%foo2;002;bar2%foo3;003;bar3%";

//split each record into an array
var myRecords = myString.split("%");

Now that I have each record in an array, I need to load each array
element into a data object property as follows:

myData[0] = {prop1:"foo1",prop2:"001",prop3:"bar1"};
myData[1] = {prop1:"foo2",prop2:"002",prop3:"bar2"};
myData[2] = {prop1:"foo3",prop2:"003",prop3:"bar3"};

Is there a way to go from the string straight to the data object, or
should I use the above process? How can I split each array element into
3 properties for the data object? Thanks.

Nov 23 '05 #1
1 1412
samseed wrote:
Hi, I have a delimited string that I would like to load into a data
object. Here is what I have so far:

//single string of data; each record is delimited by "%"
//and each value within each record is delimited by ";"
var myString = "foo1;001;bar1%foo2;002;bar2%foo3;003;bar3%";
The extra '%' at the end will create an empty element in myRecords when
you split myString. You should either make sure that never happens, or
modify the conversion routine to deal with it appropriately.

//split each record into an array
var myRecords = myString.split("%");

Now that I have each record in an array, I need to load each array
element into a data object property as follows:

myData[0] = {prop1:"foo1",prop2:"001",prop3:"bar1"};
myData[1] = {prop1:"foo2",prop2:"002",prop3:"bar2"};
myData[2] = {prop1:"foo3",prop2:"003",prop3:"bar3"};

Is there a way to go from the string straight to the data object, or
should I use the above process? How can I split each array element into
3 properties for the data object? Thanks.

var myString = "foo1;001;bar1%foo2;002;bar2%foo3;003;bar3";
var myRecords = myString.split("%");
var temp, myData = [];
for (var i=0, m=myRecords.length; i<m; ++i){
myData[i] = {};
temp = myRecords[i].split(';');
for (var j=0, n=temp.length; j<n; ++j){
myData[i]['prop'+j] = temp[j];
}
}

Should do the trick. If the delimiters appear anywhere in myString that
they shouldn't, myData will likely be junk.

--
Rob
Nov 23 '05 #2

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

Similar topics

12
2844
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
0
4847
by: RSH | last post by:
I am having a bit of trouble filling a datatable with a query. I'm getting the error message: System.FormatException: String was not recognized as a valid DateTime. at...
9
7110
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
3
3970
by: Rich | last post by:
Hello, I am converting an app from VB6 to VB.Net and have encountered the following problem. I have the following loop which retrieves objects from a collection of objects. Dim entry As...
17
2187
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
1
5212
by: macupryk | last post by:
+ ex {"The name \"CHARACTER\" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted."}...
14
4251
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
1
6038
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
0
2763
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the...
0
1523
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
7093
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...
1
7011
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
7468
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...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.