Solved

Array of string in Javascript

Posted on 2011-03-01
6
282 Views
Last Modified: 2012-05-11
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
Comment
Question by:pratz09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 35013992
>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
 
LVL 82

Expert Comment

by:leakim971
ID: 35014003
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
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 200 total points
ID: 35014004
make it

int j=0;
while (j < Maximum) {
            slider[j] = "'id': 'slide-img-" + (j+1) + "', 'client': '', 'desc': ''";
            j++;
        }
0
Technology Partners: 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!

 

Author Comment

by:pratz09
ID: 35014019
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
 
LVL 82

Expert Comment

by:leakim971
ID: 35014029
>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
 

Author Closing Comment

by:pratz09
ID: 35015719
Thank a lot guys.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question