• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Array of string in Javascript

I am novice enough in JS to not to be able to solve this silly problem.

I wrote these statements to statically allocate value to string array inside javascript tag:

var slider = {}; slider.data = [{ "id": "slide-img-1", "client": "", "desc": "" }, { "id": "slide-img-2", "client": "", "desc": "" }, { "id": "slide-img-3", "client": "", "desc": "" }, { "id": "slide-img-4", "client": "", "desc": ""}];

I worked like a charm, but now I need to insert value into the array dynamically and I wrote this:

var slider = new Array();
int j=0;
while (j < Maximum) {
            slider[j] = "\"id\": \"slide-img-" + (j+1) + "\", \"client\": \"\", \"desc\": \"\"";
            j++;
        }
        slider[j] = "\"id\": \"slide-img-" + (j+1) + "\", \"client\": \"\", \"desc\": \"\"";

Believe me I am not this stupid, but I am just so tired and got to get done with this soon. Do I need to put those escape sequences before double quotes ? Help is greatly appreciated.
0
pratz09
Asked:
pratz09
  • 3
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
>Do I need to put those escape sequences before double quotes ?

No, use :


var slider = {};
		slider.data = [];
		//slider["data"] = [];
		var Maximum = 4;
		for(var j=1;j<=Maximum;j++) {
		   slider.data.push( { "id": "slide-img-" + j, "client": "", "desc": "" } );
		}

Open in new window

0
 
leakim971PluritechnicianCommented:
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload = function() {
		var slider = {};
		slider.data = [];
		//slider["data"] = [];
		var Maximum = 4;
		for(var j=1;j<=Maximum;j++) {
		   slider.data.push( { "id": "slide-img-" + j, "client": "", "desc": "" } );
		}
		
		// checking :
		
		alert( slider.data[0].id );
		alert( slider["data"][1].id );
		alert( slider.data[2]["id"] );
		alert( slider["data"][3]["id"] );
		
	}
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
Gurvinder Pal SinghCommented:
make it

int j=0;
while (j < Maximum) {
            slider[j] = "'id': 'slide-img-" + (j+1) + "', 'client': '', 'desc': ''";
            j++;
        }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pratz09Author Commented:
Did not work without escape sequences either... There's something very tiny i am missing ? Wait, it is multi dimensinal array, is the declaration i did in second case wrng ?
0
 
leakim971PluritechnicianCommented:
>Wait, it is multi dimensinal array, is the declaration i did in second case wrng ?

Yes, currently you try to add them like string not as object (multi dimensinal array)
0
 
pratz09Author Commented:
Thank a lot guys.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now