How to get an array in an object / jQuery plugin properties array

Probably a simple question, I just never did this ... I'm trying to figure out how you will populate the jQuery plugin object / properties array or whatever you call it ... with a regular array.

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

So, I'm trying to get this in ... a property of the "image" plugin with an unknown number of images ...

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

Here is an example of what I'm trying to do ... although I do not know the correct syntax ...

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

Any help is appreciated !!!

+4
source share
3 answers

Using $.map :

 var imageArray = ['images/1000x800.png','images/495x880.png', ...]; $.myPlugin({ slides: $.map(imageArray, function (el) { return {image: el}; }); }); 
+2
source

That should transform 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 .

Although note that this converts the original array. If you want a second one for this, just create a new object with a var new = [] and new.push() object inside the loop.

+1
source

Why not just define the array you need and not define and convert it?

 var imageArray = [ { image: 'images/1000x800.png' }, { image: 'images/495x880.png' }, { image: 'images/600x800.png' } ]; jQuery(function($) { $.myplugin( { slides: imageArray } ); }); 
0
source

Source: https://habr.com/ru/post/1345643/


All Articles