Jquery, how to loop inside repeater setlist?

2.2k Views Asked by At

I have code as below,

var $repeater = $('.repeater').repeater();
$repeater.setList([
    { 'text-input': 'A' },
    { 'text-input': 'B' },
    { 'text-input': 'c' },
    //and so on...
]);

but the problem is, I have no idea how to loop the { 'text-input': 'A' }, Lets say I have 10 of this { 'text-input': 'A' }, so by right I do my code like this below but it produce syntax error.

var $repeater = $('.repeater').repeater();

$repeater.setList([
    for (var i = 0; i < 10; i++) {

        { 'text-input': i },

    }
]);
2

There are 2 best solutions below

0
On BEST ANSWER

This would be the code using simple javascript loop:

var $repeater = $('.repeater').repeater();

var list = [];

for (var i = 0; i < 10; ++i) {

    list.push({ 'text-input': i });

}

$repeater.setList(list);
0
On

You can use Array.from with a map function to generate an array.

$repeater.setList(
  // specify the length of the array you need
  // within the map function second argument refers the index
  Array.from({ length: 10 }, (_, i) => ({ 'text-input': i }))
);