Array (i)--array creation and manipulation

Source: Internet
Author: User
Tags array length

Create an array of 1. Literal quantity
2. New Array
var arr = new Array();var arrWithLength = new Array(100);//undefined*100var arrLikesLiteral = new Array(true,false,null,1,2,‘hi‘);//等价于[true,false,null,1,2,‘hi‘];

Array length limit range: 0~2 23 times-1.

Array Read and write
var arr = [1,2,3,4,5];arr[1];//2arr.length;//5arr[5] = 6;//可以动态添加数组长度arr.length:;//6
Array elements are dynamically deleted, without specifying the size
//数组尾部增加元素arr.push();arr[arr.length] = 4;//equal to arr.push(4);//数组头部增加元素arr.unshift(0);var = [1,2];delete arr[0];//删除索引,值变为undefinedarr[0];//undefined , 长度不变。arr.length;//20 in arr;//false,0索引已被删除,如果不执行delete,主动为arr[0]设置值为undefined,0 in arr将得到true。//数组尾部元素删除arr.pop();arr.length--;//equal to arr.pop();//数组头部元素删除arr.shift();
Array Iteration 1. Normal for loop
var i = 0,n=10;var arr = [1,2,3,4,5];for(;i<n;i++){    console.log(arr[i]);}
2. For in (not recommended)

Iterates over the properties on the prototype chain

var i = 0,n=10;var arr = [1,2,3,4,5];for(i in arr){    console.log(arr[i]);}
3. For
var i = 0,n=10;var arr = [1,2,3,4,5];for(i of arr){    console.log(i);}
4.array.prototype.foreach
var arr = [1,2,3,4,5];arr.forEach(function(value,index,arr){    console.log(value);})

The index attribute is present if the array element is considered to be undefined, such as arr[0] = undefined, if the index attribute is not present (although the array elements inside are undefined), but the size is specified for the arrays, and no value is set.

Array (i)--array creation and manipulation

Related Article

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.