Jquery make array on the fly using the value of a variable

Posted on 2013-10-21
Medium Priority
Last Modified: 2013-10-22

I run the following code

var pagePath = "http://www.xxx.com/multimedialist.txt";
$j('#textLoader').load(pagePath, function (responseText) {
var multimediaListContents = responseText;

Open in new window

so now i have a variable which has the exact contents of my multimedialist.txt file:
var multimediaListContents = "unipol,2EksseaXwbQ|altaformazione,u8lpELxse5M"

Open in new window

I would like to use jquery to create two arrays on the fly as follows:

var videoIconName = [];
videoIconName[ 0 ] = "unipol";
videoIconName[ 1 ] = "altaformazione";

var videoLocation = [];
videoLocation[ 0 ] = "2EksseaXwbQ";
videoLocation[ 1 ] = "u8lpELxse5M";

Open in new window

How could I achieve this?

Thanks in advance!
Question by:badwolfff
  • 2
LVL 34

Expert Comment

by:Big Monty
ID: 39587944
dont really need to use jQuery, straight up JavaScript is pretty simple:

var tmpArr = multimediaListContents.Split( '|' );
var videoIconName = tmpArr[0].Split( ',' )
var videoLocation = tmpArr[1].Split( ',' )

Open in new window


Author Comment

ID: 39588046

thanks for the message.
I tried your code and I get a wrong output.
What I get is this:

videoIconName[0] = "unipol";
videoIconName[1] = "2EksseaXwbQ";


videoLocation[0] = "altaformazione";
videoLocation[1] = u8lpELxse5M";

Since the txt file contained this string:

what i need is as follows:

videoIconName[ 0 ] = "unipol";
videoIconName[ 1 ] = "altaformazione";

videoLocation[ 0 ] = "2EksseaXwbQ";
videoLocation[ 1 ] = "u8lpELxse5M";

Also I used the jQuery loop to ensure that the two arrays got all the right content from the txt file. If I don't use the array and there are 20 sets of values present in the, txt then I would need to do them manually.

thanks again
LVL 45

Accepted Solution

Chris Stanyon earned 2000 total points
ID: 39588150
Assuming you can use the pipe and comma for the delimiters, then something like this:

var pagePath = "http://www.xxx.com/multimedialist.txt";

$('#textLoader').load(pagePath, function (responseText) {
	var multimediaListContents = responseText.split("|");
	var videoIconName = new Array();
	var videoLocation = new Array();
	$(multimediaListContents).each(function(i,elem) {
		var temp = elem.split(",");

Open in new window


Author Comment

ID: 39590295
Thanks ChrisStanyon!

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn how to dynamically set the form action using jQuery.

627 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