4

Probably easy question, I've just never done this before... I'm trying to figure out how you would populate a jQuery plugin property object/array or whatever you call it... with a normal array.

var imageArray = new Array();
imageArray = ['images/1000x800.png','images/495x880.png','images/600x800.png'];

So, I trying to get that into... the plugin property "image" with an unknown amount of images...

jQuery(function($){
  $.myPlugin({
    slides : [
      {image : 'images/1000x800.png'},
      {image : 'images/495x880.png'},
      {image : 'images/600x800.png'},
      //...etc...
    ]
  });
});

Here's an example of what I'm trying to do... although I don't know the correct syntax...

jQuery(function($){
  $.myPlugin({
    slides : [ for(var i=0; i<imageArray.length; i++) {
                image = imageArray[i];
               }
    ]
  });
});

Any help appreciated!!!

3 Answers 3

2

Using $.map:

var imageArray = ['images/1000x800.png','images/495x880.png', ...];
$.myPlugin({
    slides: $.map(imageArray, function (el) { return {image: el}; });
});
Sign up to request clarification or add additional context in comments.

Comments

1

This should convert it...

var imageArray = ['images/1000x800.png','images/495x880.png','images/600x800.png'];

for(var i=0; i<imageArray.length; i++) {
   imageArray[i] = {'image': imageArray[i]};
}

jsFiddle.

Though note, this will convert the original array. If you want a second one for this, simply make a new one with var new = [] and new.push() the object inside the loop.

Comments

0

Why not just define the array you need instead of defining one and converting?

var imageArray = [
                    { image: 'images/1000x800.png' },
                    { image: 'images/495x880.png' },
                    { image: 'images/600x800.png' }
                 ];

jQuery(function($) {
    $.myplugin( { slides: imageArray } );
});

Comments

Your Answer

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.