undefinedfix
Sign in

The problem of array in native JS

Alek edited in Mon, 22 Mar 2021
Array.apply(null, Array(100)).map(function(item, i) {
    console.log(item,i)
    return i+1;
});

Item I can be printed circularly

Array(100).map(function(item,i){
    console.log(item,i)
})

// (100) [undefined × 100]

Or will it loop undefined

Array.apply Isn't (null, 100) also generating an array with a length of 100?

There will be a little difference in the console output, but these two declaration methods generate two arrays with a length of 100

图片描述

2 Replies
user1433800
commented on Mon, 22 Mar 2021
new Array(100).map(function(item,index){
    console.log(item,index)
})

There won't be any output. There is only one return value. A sparse array is created. There is no value stored in the array, and the index attribute has not been defined, so there is no index.

suxbr
commented on Mon, 22 Mar 2021

Each item of array (100) is just created, and there is no assignment

Array.apply (null, {length: 100}) is created and assigned

lock This question has been locked and the reply function has been disabled.