JS Basic--array

Source: Internet
Author: User

First, the characteristics

    1. variable data types;
    2. The size of the array can be adjusted dynamically;
    3. Has the length property, and the length property is not read-only, can be set;

Ii. Method of Creation

    1. Use the array constructor;
      var arr=new Array ();  var arr=New Array (a);  var arr=New Array ("arr1", "arr2", "ARR3");      
    2. Array literal notation
      var arr=["arr1", "arr2", "ARR3"];  var arr=[]; 

Third, the detection array

instanceof Array; Array.isarray (value); //value   is an array

Iv. Method of conversion

ToString (), tolocalestring () returns a string, separated by a

Join () returns a string that is separated from the parameters inside the join function and has no effect on the original array

ValueOf () returns an array that can be assigned to another array in this way if the array operation will have the same action on the original array.

If an item is null or undefined, it is represented by an empty string in join (), tolocalestring (), toString ()

var arr=["arr1", "arr2", "Arr3"];console.log (Arr.tostring ());  Arr1,arr2,arr3console.log (arr.tolocalestring ()); //Arr1,arr2,arr3console.log (arr.valueof ());  ["arr1", "arr2", "ARR3"], you can assign an array to another array in this way, and if you manipulate the array, you will have the same action on the original array. Console.log (arr); //["arr1", "arr2", "Arr3"]console.log (Arr.join ("| |)"); //  return string, no change to the original array
If an item is null or undefined, it is represented by an empty string in join (), tolocalestring (), toString ()

Five, Stack method

Push () returns the modified array length

Pop () returns the extracted elements, starting from arr[arr.length-1] to fetch

var arr=["arr1"];  var count=arr.push ("arr2", "ARR3"); // returns the modified array length Console.log (count);  3console.log (arr); //["arr1", "arr2", "ARR3"]var thetop=arr.pop ();  returns the extracted elements, starting from arr[arr.length-1] to fetch console.log (thetop); //ARR3 string Console.log (arr);  ["arr1", "arr2"]              

Vi. Queue Methods

Shift () returns the extracted elements, starting from arr[0] to fetch

Unshift () Increase in front, add arr[0], return the modified length

varArr=[];varCount=arr.push ("arr1", "arr2");//returns the modified array lengthConsole.log (count);//2Console.log (arr);//["arr1", "arr2"]varThebottom=arr.shift ();//returns the extracted element, starting from arr[0]Console.log (Thebottom);//arr1 StringConsole.log (arr);//["ARR2"]varCount=arr.unshift ("arr1", "arr0");//increase in front, increase arr[0]Console.log (count);//3, returns the modified lengthConsole.log (arr);//["arr1", "Arr0", "arr2"]

Seven, reorder method

Reverse reverse output, change the original array

Sort ascending, comparing strings, to solve this problem, just pass in a function in sort to

function is

Arr.sort (function (A, b) { return b-a}); b-a from big to small, a-B small to large

//ReverseReverse output
var arr=[0,1,5,10,15];arr.reverse (); // reverse output console.log (arr); [15, 5, 1, 0]
//sort ascending sort arr.sort (); // ascending sort, comparing the string Console.log (arr); //[0, 1, ten, 5]// Sort solution arr.sort ( Function (A, b) { b-a from large to small, a-B from small to large console.log (arr); //[15, 5, 1, 0]

Eight, Operation method

Concat () connection, does not affect the original array

Slice () Copy the new array without affecting the original array

Splice () Delete, operate on the original array, return an array of deleted items

//Concat connectionvar arr=["arr1", "arr2"];var arrs=arr.concat ("Arr3", ["ARR4", "ARR5"]);//does not affect the original array console.log (ARR);//["Arr1", "arr2"]console.log (ARRS);//["Arr1", "arr2", "Arr3", "ARR4", "ARR5"]//Slice copying a new arrayvar arrs1=arrs.slice (1);//var Arrs2=arrs.slice (1,4); ////["arr2", "Arr3", "ARR4", "ARR5"], does not affect the original array console.log (ARRS2); //["arr2", "Arr3", "ARR4"] //var removed =arr.splice (0,1, "arr1_1", "arr1_2"); // start deleting 1 items at 0, insert 2 console.log (arr); //["Arr1_1", "Arr1_2", "arr2"]console.log (removed); //["arr1"] returns an array of deleted Items      

Nine, location method

IndeXof looking after from the go

LastIndexOf looking from the rear

Not found in case 1

//IndexOf looking after from the tripvar arr=[1,2,3,4,5,4,3,2,1];console.log (Arr.indexof (4));//3, return 4 position Console.log (Arr.indexof ("4"));//-1, not found for case-1, press = = = Search//LastIndexOf from the tail to find Console.log (Arr.lastindexof (4));//5, return 4 where Console.log (Arr.lastindexof ("4")); //-1, not found in case 1, press = = = Find var person={name: "XXX"};< Span style= "color: #008000;" >// object var people=[{name: "XXX"}]; array, containing one item, which is an object (not related to person) var Morepeople=[person] ; ////-1,people array does not contain person objects console.log ( Morepeople.indexof (person)); //0,morepeople array with Person object, first item     


Ten, Iterative methods

Every (), each item is satisfied returns TRUE, otherwise false

Filter(), which returns an array of items that satisfy the condition, with no effect on the original array

ForEach(), equivalent to a for loop

Map(), Operations on arrays, no effect on the original array

Some(), at least one satisfy returns true, otherwise false

var numbers=[1,2,3,4,5,4,3,2,1];//Every, each item is satisfied then returns TRUE, otherwise falsevar everyresult=numbers.every (function(Item,index,array) {Return (item>2);}); Console.log (Everyresult);//False//Filter, which returns an array of items that satisfy the condition, with no effect on the original arrayvar filterresult=numbers.filter (function(Item,index,array) {Return (item>2);}); Console.log (Filterresult);//[3, 4, 5, 4, 3]console.log (numbers);//[1,2,3,4,5,4,3,2,1]//ForEach, equivalent to A For loop Numbers.foreach (function(Item,index,array) {//Perform certain actions});////Map, operations on arrays, no effect on the original arrayvar Mapresult=numbers.map (function (Item,index,array) {return (Item*2// [2, 4, 6, 8, ten, 8, 6, 4, 2]console.log (numbers); //[1,2,3,4,5,4,3,2,1]// Some returns true if at least one of the items is satisfied, otherwise returns Falsevar Someresult=numbers.some ( function (Item,index,array) { return (Item>2//true        

Xi. method of merging

Reduce (), first time prev=1,cur=2 (second item), second time prev=3 (1+2), cur=3 (third item)

Reduceright (), first prev=5,cur=4 (fourth), second time prev=9 (5+4), cur=3 (third item)

Reduce (), and Reduceright () traverse the same direction, except for the same

var numbers=[1,2,3,4,5];//Reduce, first time prev=1,cur=2 (second item), second time prev=3 (1+2), cur=3 (third item)var sum=numbers.reduce (function  (Prev,cur,index,array) {return Prev+cur;}); Console.log (sum); //15//reduceright, first time prev=5,cur=4 (fourth), second time prev=9 (5+4), cur=3 (third item) var sum=numbers.reduce (function (Prev,cur,index,array) {return prev+cur;}); Console.log (sum); //15//reduce, and reduceright traverse direction are different, except for the same           

JS Basic--array

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.