Array null value empty

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.