Summarizes various operations of Javascript on array objects, and summarizes javascript

Source: Internet
Author: User
Tags array definition

Summarizes various operations of Javascript on array objects, and summarizes javascript

Array

Array definition: in short, it is a group of ordered data sets. Its index is an integer that starts from 0 and naturally increases. Its element value can be any js data! It also contains an attribute named length, which indicates the number of array elements!

I. define arrays. There are three definition methods:

Method 1:

var arr=new Array(); arr[0]=“11”; arr[1]=“22”; arr[2]=“33”; 

Method 2:

var arr=new Array(“11”,“22”,“33”); 

Method 3:

var arr=[“11”,“22”,“33”]; 

2. The length attribute of the array can be used to obtain the length of the array, or intercept and clear the array. If the set value is smaller than the current value, the array will be truncated, the elements at the end of it will be lost.

If the set value is greater than the current value, the length of the array will increase, and new elements will be added to the end of the array. Their values are undefined:

Var arr = ["11", "22", "33"]; arr. length // 3, returns the length of the array arr. length = 2 // ['11', '22'], truncates the first two arr. length = 5 // ['11', '22', undefined], automatically add undefined arr. length = 0 // [], clear the Array

3. traverse the array:

Var arr = ["11", "22", "33"]; // for Loop (var I = 0; I <arr. length; I ++) {console. log (I)} // for in loop for (I in arr) {console. log (arr [I])} // forEach loop arr. forEach (function (I) {console. log (I )})

4. add and delete Arrays:

push()Add one or more elements to the end of the array. The returned value is the length of the array after the element is added.

var arr=[1,2,3]; arr.push(4) console.log(arr) //[1,2,3,4] arr.push(5,6,7) console.log(arr) //[1,2,3,4,5,6,7] 

unshift() Add one or more elements to the beginning of the array. The returned value is the length of the array after the element is added.

var arr=[1,2,3]; arr.unshift(4) console.log(arr) //[4,1,2,3] arr.unshift(5,6,7) console.log(arr) //[5,6,7,1,2,3,4] 

pop()Delete from the end of the array. The returned value is the value of the deleted element.

var arr=[1,2,3]; arr.pop() console.log(arr) //[1,2] 

shift() Delete from the beginning of the array. The returned value is the value of the deleted element.

var arr=[1,2,3]; arr.shift() console.log(arr) //[2,3] 

5. join () separates arrays by specified delimiters. the return value is of the string type, without changing the original array:

var arr=[1,2,3,4]; arr.join(‘-‘) //”1-2-3-4″ arr.join(”) //”1234″ arr.join(‘ ‘) //”1 2 3 4″ 

6. sort the sort () array:

Var arr = [,]; // arr is sorted from small to large. sort (function (a, B) {return a-B;}); // sort arr from large to small. sort (function (a, B) {return B-a;}); // arr. sort (function (a, B) {return Math. random ()-0.5 })

7. reverse () sorts the array in reverse order:

var arr=[2,8,3,4,12,56]; arr.reverse() //[56, 12, 4, 3, 8, 2] 

8. Obtain the largest and smallest numbers in the array:

var arr = [5, 458 , 120 , -215 , 228 , 400]; var max = Math.max.apply(Math, arr); var min = Math.min.apply(Math, arr); 

9. slice () can return the selected elements from the existing array without changing the original array.

A parameter starting from the start subscript to the end.

There are two parameters: array elements from start subscript to end subscript (excluding this element:

var arr=[2,8,3,4,12,56]; arr.slice(1) //[8, 3, 4, 12, 56] arr.slice(1,5) //[8, 3, 4, 12] 

10. splice ()

A parameter that is used to delete the subscript from start to end. Returns the number of deletions. This directly modifies the original array.

There are two parameters: Delete the array elements from the start subscript to the end subscript and return the number of deleted elements. This directly modifies the original array.

There are three parameters. Replace the element from the start subscript to the end subscript with the third parameter. If the first two values are the same, replace the element. directly modify the original array:

Var arr = [,]; // captures the console from the position of subscript 2. log (arr. splice (2) // [3, 4, 12, 56] console. log (arr) // [2, 8] var arr = [,]; // Delete the console where the subscript ranges from 1 to 5. log (arr. splice (1, 5) // [8, 3, 4, 12, 56] console. log (arr) // [2] var arr = [,]; // Replace the location console of subscript 1. log (arr. splice (1, 1, 'qqq') // [8] console. log (arr) // [2, "qqq", 3, 4, 12, 56] var arr = [,]; // Delete the subscripts from 1 to 3 and insert them to the qqq console. log (arr. splice (1, 3, 'qqq') // [8, 3, 4] console. log (arr) // [2, "qqq", 12, 56]

11. concat () can combine two arrays into a new array and return:

var arr1=[1,2,3,4,5]; var arr2=[6,7]; var arr3=arr1.concat(arr2); alert(arr1);// [1,2,3,4,5] alert(arr2);// [6,7] alert(arr3);// [1,2,3,4,5,6,7] 

12. array deduplication:

Method 1:

function removeRepeat(arr){  return arr.filter(function(elem, pos) {   return arr.indexOf(elem) == pos;  }); } 

Method 2:

function removeRepeat(a){  var arr=[];  for(var i=0;i<a.length;i++){   if(arr.indexOf(a[i]) === -1){    arr.push(a[i]);   }  }  return arr; } 

Method 3:

function removeRepeat(a){  var arr = [];  a.forEach(function(i){    if(arr.indexOf(i) === -1){    arr.push(i);   }  });  return arr } 

Method 4:

function removeRepeat(arrs){  var newArr = [];  var hash = {};  for(var i=0;i<arrs.length;i++){   var key = typeof(arrs[i])+arrs[i];   if(hash[key] !==1){    newArr.push(arrs[i]);    hash[key] =1;   };   };  return newArr; } 

12. prototype attribute. directly modify or add the array prototype:

// For example, we add a summation method Array to the Array. prototype. sum = function () {var n = 0; this. forEach (function (I) {n + = I;}); return n;} var arr = [1, 2, 4] alert (arr. sum () // 10

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

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.