0

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 },

    }
]);
Aditya Sharma
  • 645
  • 1
  • 10
  • 28
LearnProgramming
  • 814
  • 1
  • 12
  • 37

2 Answers2

2

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 }))
);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

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);
Adder
  • 5,708
  • 1
  • 28
  • 56