Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array of string in Javascript

Posted on 2011-03-01
6
Medium Priority
?
291 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
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.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

636 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