2

I still for my own education want to see an elegant jQuery version of a construct like this - Note the image filenames are 1 based and not 0 based :

var nofImages = 10; // user defined
var slideShowArray = new Array(nofImages); // cannot use [nofImages] of course
for (var i=0, n=slideShowArray.length;i<n;i++) {
 slideShowArray[i]="/images/image"+(i+1)+".jpg";
}

or perhaps according to the above mentioned articles it should be

var nofImages = 10; // user defined
var slideShowArray = [];
for (i=nofImages;i>0;i--) {
 slideShowArray[(i-1)]="/images/image"+i+".jpg";
}

Thanks

asked Dec 15, 2010 at 7:21
0

2 Answers 2

4
var slideShowArray = $.map(new Array(10), function(i,j) {
 return '/images/image'+(j+1)+'.jpg';
});

In Javascript 1.8 you will be able to do this more elegantly:

var slideShowArray = $.map(new Array(10), function(i,j) '/images/image'+(j+1)+'.jpg');

or even

$.range = function(first,last,step) {
 step = step || 1;
 if (typeof last == undefined) {
 last = first;
 first = 0;
 }
 return $.map(Array((first-last)/step), function(i,j) {
 return j*step + first;
 });
}
var slideShowArray = ['/images/image'+i+'.jpg' for (i in $.range(1,10))];
answered Dec 15, 2010 at 8:43
Sign up to request clarification or add additional context in comments.

3 Comments

That is more what I was looking for. In your example the "i" is not interesting since it is just a necessary placeholder for the empty initialised array element, right?
Indeed, first parameter is array element, second is array index. You could write function(_,i) {...+(i+1)+...} if you like that sort of stuff.
Great. Not sure I like the 1.8 versions at all.. I want my curly brackets ;)
2

Trying to be a jquery purist you might want to try the .map() method.

var noOfImages = 10;
var slideShowArray = [];
slideShowArray[noOfImages - 1] = ""; // set initial size
slideShowArray = $.map(slideShowArray, function(element, index) {
 return "/images/image"+index+".jpg"
});

But I think this would be simpler:

var noOfImages = 10;
var slideShowArray = [];
for(var i = 0; i < noOfImages; i++) 
 slideShowArray.push("/images/image"+i+".jpg");

(Note that your examples are a bit broken - setting the array to [noOfImages] sets the length to 1.)

answered Dec 15, 2010 at 7:31

4 Comments

Sorry. Mixed my initialiser from new Array(nofImages) and []. However your second example is another non-jquery example. It is broken too ;) It does not use 1 based image names. I wanted to find a plausible jQuery each-type script to see if it was too horrible to use or not ;)
"Initializing" the array by setting the last element is not safe - the current behavior of map might be to walk from 1 to the last index, and not skip unset indexes, but there is no promise in the jQuery API to behave that way.
@mplugjan: nope, your image names are broken ;)
Haha, Not mine. I always use 0 based names or SEO best practise names

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.