When the array constructor takes only one numeric argument (otherwise as a fill), the parameter is used as the preset length of the array instead of filling an element, so the array is empty cells
If an empty cell exists in an array, that is, the value of length is greater than the actual number of cells, such an array is called a sparse array
var= (newArray(3// 目前chrome返回[empty * 3], 之前好像是[undefined * 3]// new可用可不用,不用的话内部会自动添加var= [undefined,undefined,undefined]var= []c.length=3// 目前chrome返回[empty * 3], 之前好像是[undefined * 3]
A,b,c is consistent in terms of previous return values, but empty cells are not the same as undefined
a.Map(function(V,I{ returnI; }); //Current Chrome returns [Empty * 3], previously [undefined * 3]b.Map(function(V,I{ returnI; }); //[0, 1, 2]varD=[' A ', ' B ', ' C ', ' d ']Deleted[1]D.Map(item,Index= { return {Item,Index, }})//(4) [{item: "A", index:0}, empty, {item: "C", Index:2}, {item: "D", index:3}]
Fortunately, the return is empty, not undefined, and can be identified.
However, some methods, such as join, are consistent with undefiend in treating empty cells.
a.Join('-'); // "--"b.Join('-'); // "--"//Implementation reference as followsfunction Fakejoin(arr,Connector{ varStr= ""; for(varI= 0;I< arr.length;I++){ //It mainly considers the length of the array, which is the key if(I> 0){Str+=Connector; } if(Arr[i]!== undefined){Str+=Arr[i]; } } returnStr;}
You can use the following expression to populate undefined in an array instead of empty
var=Array.apply(null,{length:3}// [undefined, undefined, undefined]// apply第二参数要求是数组或类数组(存在length属性)
ES6 added the Array.of () method to initialize the array, in cases where only one argument is filled as an array instead of set length
varArray1= Array. of(3)//[3]varArray2= Array. of(1,2,3)//[1,2,3]//Simulationfunction Arrayoflike(){ return[].Slice.Pager(arguments)//return [... arguments] ES6}//orfunction Arrayoflike(...Rest){ returnRest}
Array null value empty