Declaring array of objects

前端 未结 15 1448
遇见更好的自我
遇见更好的自我 2020-12-02 07:01

I have a variable which is an array and I want every element of the array to act as an object by default. To achieve this, I can do something like this in my code.



        
15条回答
  •  佛祖请我去吃肉
    2020-12-02 07:34

    You can instantiate an array of "object type" in one line like this (just replace new Object() with your object):

    var elements = 1000;
    var MyArray = Array.apply(null, Array(elements)).map(function () { return new Object(); });
    

提交回复
热议问题