Replace a hard-coded list of numbers with dynamic array length

I'm trying to figure out how to replace the values ​​1,2 and 3 inside this GUI with a range of numbers based on a simple number of objects in an array so that it dynamically tracks it without the need for a hardcore in every update of the array. enter image description here

What prevents me from simply using the array with the numbers themselves is that each of the numbers is an object inside this GUI. The line of code is this.

gui.add(data, 'system', {
  "1": 0,
  "2": 1,
  "3": 2
})

Suppose I use any random array containing any random information, for example:

var arr = [];
for (var i = 0; i<51;i++){
    arr.push("element");}

51 , , , , "1", "2", "3", "4", "5", "6" ( 10, 1 ) , . , , 10, , 51 , , 6, .

? . . FIDDLE

EDIT: , . 1,2,3, :

gui.add(data, 'system', {
  "1": 0,
  "2": 1,
  "3": 2
}).name('system #').onChange(function(value) {
  updateSets(value);
});

, , , "1", "2", "3", (, 3 ) . 51 , 51 , 10, 6. 5, 1.

+4
1

Array.prototype.reduce() .length .

. Object.entries(), for..of , n.

Array.prototype.slice(), n x .

var arr = [];
for (var i = 0; i < 51; i++) {
  arr.push("element");
}

var obj = arr.reduce(function(obj, element, index) {
  obj[index + 1] = index;
  return obj
}, {});

function getObjProps(from, to, obj, res) {
  let it = void 0;
  if (from) {
    it = Object.entries(obj).slice(from, to);
  } else {
    it = Object.entries(obj);
  }
  for (let [key, value] of it) {
    if (+key <= to) {
      res[key] = value;
    } else {
      break;
    }
  };

  return res
}


var curr = getObjProps(null, 6, obj, {});

console.log(curr);

var next = getObjProps(6, 12, obj, {});

console.log(next);

for (let i = 12; i < arr.length; i += 6) {
  console.log(getObjProps(i, i + 6, obj, {}))
}
Hide result
+1

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


All Articles