0

I am struggling with the correct syntax for iterating through an array to pass properties (and number of items) to an external JS constructor function.

The syntax for the constructor is:

`swal.withForm({
 formFields: [
          { id: 'X', type:'Y', name:'Z', value:''},
          {etc...} ]
})`

But let's say that the number of formFields is dynamic, and they are stored in an array. How can I iterate through that inside the constructor function? Or is there a way to "paste" the externally created syntax into the function?

Hope this question makes sense.

Samane
  • 500
  • 7
  • 23
  • have a look at http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript but it may be that you need to get your head around the use of functions as parameters .. is JQuery an option? – Peter Scott Oct 03 '15 at 08:52

1 Answers1

0

Do you mean you want to populate the formFields array with some dynamic data?

If so this should do it.

var myFields = [];

myFields.push({ id: 'X1', type:'Y', name:'Z', value:''});
myFields.push({ id: 'X2', type:'Y', name:'Z', value:''});
myFields.push({ id: 'X3', type:'Y', name:'Z', value:''});

swal.withForm({
 formFields: myFields
})

You could also build the array using a loop or the map function if you have another array you need to convert into this formFields array.

Lenny
  • 5,663
  • 2
  • 19
  • 27