Javascript: split array into parts, loop through and deal with each part

Below is a typical array that i need to send on GET, its failing because the resulting URL is too long...

How can i split this array at "Planet" then loop through and deal with each sub Array accordingly


Thank you
Planet,Yggdrasil,[1:28:7],1,28,7,2,200,000,900,000,90,446,WODAN,,,,,,,,,,,,,,,,,2,000,2,000,,9,60,1,,3,1,17,8,6,0,26,23,17,12,12,11,11,8,6,10,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Asgard,[1:28:10],1,28,10,2,234,120,340,059,529,118,WODAN,,,,,,2,40,1,,,,,,,,,4,960,4,,0,50,1,,0,1,40,9,7,5,24,20,19,12,12,11,11,8,6,10,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Willow,[1:28:9],1,28,9,2,990,148,1,041,945,252,318,000,4,271,,,,3,257,2,,,,,,,,,3,713,231,25,1,17,1,,,,,9,6,5,29,26,17,15,13,12,14,8,9,12,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Yggdrasil,[1:28:7],1,28,7,2,200,000,900,000,90,436,WODAN,,,,,,,,,,,,,,,,,2,000,2,000,,9,60,1,,3,1,17,8,6,0,26,23,17,12,12,11,11,8,6,10,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Valhalla,[1:28:8],1,28,8,798,603,689,583,489,682,WODAN,,,,,,,,,,,,,,,,,4,116,717,,2,60,1,,9,1,18,9,6,4,27,23,21,12,12,11,11,8,6,10,10,,,,DEFENSES:,BUILDINGS:,TECHS:Moon,Luxury,[1:28:6],1,28,6,0,0,19,819,000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,15,13,12,14,8,9,12,10,4,0,1,DEFENSES:,BUILDINGS:,TECHS:Planet,Capitol,[1:28:6],1,28,6,7,809,497,2,952,897,999,491,000,14,380,1,1,1,1,2,000,3,,,16,,,,,,14,104,3,509,18,,20,1,,72,1,38,12,9,9,34,29,23,15,13,12,14,8,9,12,10,,,,DEFENSES:,BUILDINGS:,TECHS:Moon,Luxury,[1:28:6],1,28,6,0,0,19,819,000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,15,13,12,14,8,9,12,10,4,0,1,DEFENSES:,BUILDINGS:,TECHS:Planet,venus,[3:57:2],3,57,2,1,200,245,402,390,156,061,BIG%20SWEDE,,44,80,,,,,,,,3,,,1,,,2,299,80,40,44,20,1,10,20,1,51,10,8,7,29,25,21,14,12,12,13,8,9,12,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Glxy%203,[3:56:7],3,56,7,1,316,598,714,751,453,124,MUSYANG,256,,15,,,,153,,,,,,,,,,1,053,47,441,0,40,1,,8,1,42,7,8,8,23,23,19,12,12,12,11,7,7,12,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Rhea,[3:56:5],3,56,5,90,300,60,174,12,960,WRONG,,50,,,,,,,,,,,,,,,1,000,500,90,90,50,1,,30,1,10,8,7,5,21,21,15,7,6,7,8,4,4,10,7,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,Nice,[3:55:4],3,55,4,634,727,46,306,8,367,SECRET%20SQUIRREL,,2,,,,,,,,,,,,,,,100,10,10,44,20,1,,14,1,6,9,9,8,24,22,15,7,6,6,8,5,6,11,10,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,About%20Time,[3:54:13],3,54,13,82,345,46,055,12,114,SECRET%20SQUIRREL,,,,,,,,,,,,,,,,,250,76,38,36,20,1,,16,1,6,8,6,5,23,22,16,7,6,6,8,5,6,11,10,,,,DEFENSES:,BUILDINGS:,TECHS:Moon,Moon,[3:52:4],3,52,4,4,800,12,440,7,380,COMMANDER%20SISKO,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,DEFENSES:,,Planet,Deep%20Space%201,[3:52:4],3,52,4,92,164,61,401,151,732,COMMANDER%20SISKO,,,,,,,,2,,,,,,,,,1,000,200,150,100,50,1,,120,1,22,,,,,,,,,,,,,,,,,,DEFENSES:,,Planet,venus,[3:57:2],3,57,2,1,084,551,363,669,144,657,BIG%20SWEDE,,44,80,,,,,,,,3,,,1,,,2,299,80,40,44,20,1,10,20,1,51,10,8,7,29,25,21,14,12,12,13,8,9,12,10,,,,DEFENSES:,BUILDINGS:,TECHS:Moon,Moon,[3:59:10],3,59,10,0,0,0,MM269,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,11,11,11,11,9,8,11,9,0,0,0,DEFENSES:,BUILDINGS:,TECHS:Planet,Lonely,[3:59:10],3,59,10,382,848,205,447,82,667,MM269,,,,,,,,,,,,,,,,,0,0,,0,0,0,14,0,0,0,8,8,7,25,24,21,11,11,11,11,9,8,11,9,,,,DEFENSES:,BUILDINGS:,TECHS:Planet,I%27m%20not%20a%20witch%20,[3:53:10],3,53,10,73,205,34,276,17,768,BOOFY,,116,,,,,,,,,,,,,,,852,138,5,,20,,,,,,5,4,2,19,17,12,7,0,0,8,6,0,9,7,,,,DEFENSES:,BUILDINGS:,TECHS:

Open in new window

LVL 3
QuinnDesterAsked:
Who is Participating?
 
JRandelCommented:
I'm assuming that you have a long array of single elements, that are actually made up of multiple planets that you need to split by.

The below should do the trick. The resulting array will be numerically ordered by panet number from 0, then by array key, again from 0. What you'll get is a twl level multi dimensional array with each planet in their own key.


<script>
// put your planets here
var $planets = // array of elements

// ignore the rest
var $result = new Array();
var $planetNo = 0;
var $count = 0;
var $value = 0;

for(var i=0; i < $planets.length; i++) {
	$value = $planets[i];

	if ($count == 0) {
		$result[$planetNo] = new Array();
	}
	
	$result[$planetNo][$count] = $value;

	$count++;

	if ($value == 'Planet') {
		$planetNo++;
		$count = 0;
	}
	
}
</script>

Open in new window

0
 
leakim971PluritechnicianCommented:
By array you mean each object separate by a comma?

[1:28:7]
1
28
7
2
200
000


0
 
Zlatin ZlatevTechnical ArchitectCommented:
Try assigning the planets string to a string variable (e.g. "planets"), and executing split on it, like

planets.split("Planet,");
0
 
QuinnDesterAuthor Commented:
Excellent Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.