?
Solved

Array of string in Javascript

Posted on 2011-03-01
6
Medium Priority
?
286 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 1200 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 800 total points
ID: 35014004
make it

int j=0;
while (j < Maximum) {
            slider[j] = "'id': 'slide-img-" + (j+1) + "', 'client': '', 'desc': ''";
            j++;
        }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

762 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