Solved

Array of string in Javascript

Posted on 2011-03-01
6
270 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
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
Comment Utility
>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
Comment Utility
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
Comment Utility
make it

int j=0;
while (j < Maximum) {
            slider[j] = "'id': 'slide-img-" + (j+1) + "', 'client': '', 'desc': ''";
            j++;
        }
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pratz09
Comment Utility
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
Comment Utility
>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
Comment Utility
Thank a lot guys.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now