The problem of javascript array push

var I is a variable,
var arr = [];
I want to implement that I is a number, so I add the empty object {name:""} of name to my empty array a few times.
for example, if I is 1
, then arr = [{name:""}]
such as I is 2
arr = [{name:""}, {name:""}]
such as I is 3
arr = [{name:""}, {name:""}, {name:""}]
.

Apr.16,2021

let generateArray = function(i) {
    let arr = [];
    while(i--) {
        arr.push({ name: '' });
    }
    return arr;
}
generateArray(3);

is that so?


new Array(10).fill({name:''})

there is a problem with the above method. All values in the array point to the same reference. Change to:

Array.from({length:10}, () => ({name:''}))

new Array(10).fill(0).map(() => ({name: ''}))
Menu