0

I have this object

var containers = {
    1: [],
    2: [],
    3: [],
    4: [],
    5: []
};

and this object only works, if I will have 5 containers. Is it possible to have something like this

var domContainers = $(".container");

var containers = {};

for(var i = 1; i < domContainers.length; i++){
// add a new property to the object
// i: []
}
peterHasemann
  • 1,550
  • 4
  • 24
  • 56

2 Answers2

1

Yes, absolutely:

for(var i = 1; i <= domContainers.length; i++){
  containers[i] = [];
}

You will have to loop domContainers.length times, since you're starting from 1.

31piy
  • 23,323
  • 6
  • 47
  • 67
1

Yes, it's possible!:

var domContainers = [{}, {}, {}, {}];
var containers = {};
for(var i = 1; i <= domContainers.length; i++) {
    containers[i] = [];
}
console.log(containers);
Faly
  • 13,291
  • 2
  • 19
  • 37