0

Hello everyone,

I hate to be a bother, but I'm having some difficulty figuring this out.

Through an AJAX implementation, I am receiving a SQL Query result that has:

  1. An object's attribute delimited by a comma
  2. An entire object (database row) delimited by a colon

This is an example response to make it more clear for you. :)

1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108;

What I am trying to achieve is placing all of this data into an array.

I've set up a little test bed to try and get this to work; this is all I have so far:

var testString = "1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108";
var testArray = new Array();

testArray = testString.split(";");

for(i = 0; i < testArray.length; i++)
{
	document.write("<br /> Element " + i + " = " + testArray[i]); 
}

var testArray2 = new Array();

My goal is to store each ROW into an element of the array, and then RE-PARSE each element for each attribute:

This is what I have so far:

Element 0 = 1,Jeremy,130,80
Element 1 = 2,Lauren,370,300
Element 2 = 3,Jeancarlos,200,200
Element 3 = 4,Luke,330,70
Element 4 = 5,Bloom,392,108

Please help me get a multi-dimensional array that has information for each person. Example:

FinalArray[0][0] = 1
FinalArray[0][1] = Jeremy
FinalArray[0][2] = 130
FinalArray[0][3] = 80
FinalArray[1][0] = 2


You will really help with my development. Thanks so much!

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Airshow
0
var testString = "1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108";
var testArray = testString.split(";");

for(var i = 0, limit=testArray.length; i < limit; i++)
{
	testArray[i] = testArray[i].split(",");
}
0

Hi, try this:

var finalArray = new Array;
var testString = new Array;

testString[0] = "1,Jeremy,130,80;2,Ludwig,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Someoneelse1,392,108";
testString[1] = "1,William,110,30;2,Lauren,370,300;3,Sophie,200,200;4,Charlotte,330,70;5,Someoneelse2,392,108";
// You can go on forever with adding strings here....

for (x = 0; x < testString.length; x++) {

var testArray = testString[x].split(";");

for(i = 0; i < testArray.length; i++)
{
var splitArray = testArray[i].split(","); // This return for example 1,Jeremy,130,80 of which splitArray[0] = 1
// splitArray.length) is 4 in the above example
finalArray[i] = new Array;
for (d = 0; d < splitArray.length; d++) {
finalArray[i][d] = splitArray[d];
// Use this to test the things that are returned:
// alert(finalArray[i][d]);
}
}
}

~G

0

Thanks Graphix, your solution with a bit of tweaking works 100%.

var testString = "1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108";
var finalArray = new Array();

var testArray = testString.split(";");

// PRINT testArray
for(x = 0; x < testArray.length; x++)
{
	
	document.write("<br /> Element " + x + " = " + testArray[x]); 
}



for (i = 0; i < testArray.length; i++)
{
	var splitArray = testArray[i].split(",");
	finalArray[i] = new Array();
	
	for (d = 0; d < splitArray.length; d++)
	{
		finalArray[i][d] = splitArray[d];
		//alert(finalArray[i][d]);
	}
}

//PRINT finalArray
for (x = 0; x < finalArray.length; x++)
{
	for (y = 0; y < finalArray[x].length; y++)
	{
		document.write("<br /> " + finalArray[x][y]);
	}
}
0

Thanks Graphix, your solution with a bit of tweaking works 100%.

did you bother to try what I gave you? On my post testArray is the only array you need. At the end, it is a two dimensional array like you requested. No need to over complicate things like you did. If you prefer to name your array, "finalArray", than that's exactly all you need to do - rename every instance of testArray to finalArray:

var testString = "1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108";
var finalArray = testString.split(";");

for(var i = 0, limit=finalArray.length; i < limit; i++)
{
	finalArray[i] = finalArray[i].split(",");
}

//PRINT finalArray
for (var x = 0; x < finalArray.length; x++)
{
	for (var y = 0; y < finalArray[x].length; y++)
	{
		document.write("<br /> " + finalArray[x][y]);
	}
}

Edited by hielo: typo

0

No to be honest I did not even check your solution. I randomly picked one and tried it and his solution worked :)

I'll check out yours but I already have a simplified version (pretty much YOUR answer) working so it's all good.

Thanks hielo

0

To inspect the data, you could do something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Airshow :: Untitled</title>
<style type="text/css">
{}
</style>

<script>
function viewData(dataString, headingsArray, containerID){
	//Prepare the data
	var dataArray = dataString.split(";");
	for(var i=0; i<dataArray.length; i++){
		dataArray[i] = dataArray[i].split(',');
	}
	
	//Create Table
	var container = document.getElementById(containerID);
	var table = document.createElement('table');
	var thead = document.createElement('thead');
	var tbody = document.createElement('tbody');
	table.appendChild(thead);
	table.appendChild(tbody);
	var tr, th, td;
	tr = document.createElement('tr');//create row
	table.appendChild(tr);
	for(var i=0; i<headingsArray.length; i++){
		th = document.createElement('th');
		th.innerHTML = headingsArray[i];
		tr.appendChild(th);
	}
	thead.appendChild(tr);

	//Populate table with data
	for(var i=0; i<dataArray.length; i++){
		tr = document.createElement('tr');//create row
		tbody.appendChild(tr);//insert row into table
		for(var j=0; j<dataArray[i].length; j++){
			td = document.createElement('td');//create & populate cells
			td.innerHTML = dataArray[i][j];
			tr.appendChild(td);//insert cell into row
		}
		tbody.appendChild(tr);
	}
	table.setAttribute('border', 1);
	container.appendChild(table);
}

onload = function(){
	var testString = "1,Jeremy,130,80;2,Lauren,370,300;3,Jeancarlos,200,200;4,Luke,330,70;5,Bloom,392,108";
	var headings = ['Serial No.', 'Name', 'Blood Pressure', 'Age']
	viewData(testString, headings, 'dataInspector');
}
</script>
</head>

<body>

<div id="dataInspector"></div>

</body>
</html>

(I invented the headings).

Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.