Declaring array of objects

前端 未结 15 1426
遇见更好的自我
遇见更好的自我 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条回答
  •  Happy的楠姐
    2020-12-02 07:46

    After seeing how you responded in the comments. It seems like it would be best to use push as others have suggested. This way you don't need to know the indices, but you can still add to the array.

    var arr = [];
    function funcInJsFile() {
        // Do Stuff
        var obj = {x: 54, y: 10};
        arr.push(obj);
    }
    

    In this case, every time you use that function, it will push a new object into the array.

提交回复
热议问题