An array of javascript

Source: Internet
Author: User
Tags array length array to string

Arrays are used to store data in separate variable names, and you can manipulate arrays by means of methods such as adding data, deleting data, and so on.

Creating/ Accessing arrays

 By specifying the array name and the index number (the index of the array starts at 0), you can access a particular element, such as arr[2], arr[3].

1  //There are two ways to create an array: 1, create 2 with the new keyword, create by literal2  vararr =NewArray ();//create an empty array3Arr[0] = "Hello";//adding elements to the ARR array4ARR[1] = "World";//adding elements to the ARR array5 6  varARR1 =NewArray (3);//Creates an array of 3 of the length of an array7Arr[0] = "My";//adding elements to the ARR1 array8ARR[1] = "name";//adding elements to the ARR1 array9ARR[3] = "Lily";//adding elements to the ARR1 arrayTen   One  varARR2 =NewArray (1,2,3,4,5);//creating an array [1,2,3,4,5] A   -  varARR3 = [];//create an empty array -  varARR4 = [n/a];//creating an array [ +/-] the  -Console.log (arr[1]);//Back to World -Console.log (arr4[2]);//returns 3

Properties of the array

1, Length-Returns the size of the array, that is, the number of array elements

2, prototype-Returns a reference to the object type prototype

3, constructor-returns the constructor that creates the array

1 //   The Length property of the array 2var arr = [1,2,3,4,5]; 3 Console.log (arr.length);   //   returns 5

Methods of arrays

Push ()-adds one or more elements to the end of the array and returns the length of the new array (to change the original array)

Pop ()-Deletes an element at the end of the array and returns the element (to change the original array)

Unshift ()-adds one or more elements at the beginning of the array and returns the new array length (to change the original array)

Shift ()-Deletes an element at the beginning of the array and returns the element (to change the original array)

Slice ()-intercepts a fragment of an array and returns the fragment as an array (without changing the original array)

Splice ()-Adds or removes an array element at the specified position, only increments the element returns an empty array, removes the element and returns the deleted element as an array (to change the original array)

Concat ()-Joins one or more arrays and returns a new array after the connection (does not change the original array)

Reverse ()-reverses the elements in the array (to change the original array)

Sort ()-sorts the array elements, changing the original arrays (ascending by default)

Join (separator)-Converts an array element to a string separated by separator (comma delimited by default)

IndexOf ()-Returns the index of the element, a string-like IndexOf () method

1 //add element Push, Unshift, splice (change the original array)2 vararr = [A.];3Arr.push (4,5);//return 5,arr = [1,2,3,4,5]4Arr.unshift ( -1,0);//return 7,arr = [ -1,0,1,2,3,4,5]5 //Splice (Index,count,arrdata) index--increases the position (index) of the element, count--the number of elements from the index position, and arrdata--the element that is added starting at the index position .6Arr.splice (1,0,6);//return [],arr = [ -1,6,0,1,2,3,4,5]7 8 //Delete element pop, shift, splice9 vararr = [1,2,3,4,5,6,7];TenArr.pop ();//return 7,arr = [1,2,3,4,5,6] OneArr.shift ();//return 1,arr = [2,3,4,5,6] A //Splice (Index,count) index--the position (index) of the deleted element, and count--the number of elements to delete from the index position -Arr.splice (2,2);//return [4,5],arr = [2,3,6] -  the //intercepts the array slice (does not change the original array, returning the intercepted part as an array) - //Slice (Start,[end])-The Intercept array starts with start (inclusive), ends with end (not included), and if End is omitted, it is truncated from start to the end of the array - vararr = [1,2,3,4,5]; -Arr.slice (1,3);//return [2,3],arr = [1,2,3,4,5] +Arr.slice (3);//return [4,5],arr = [1,2,3,4,5] -  + //Connect array Contact (does not change the original array) A vararr = []; at varARR1 = [3,4,5]; -Arr.concat (ARR1);//return [1,2,3,4,5],arr = [up] -Arr.concat ("A", "B");//return [],arr, "A", "b" = [up] -  - //Invert array reverse (change original array) - vararr = [A.]; inArr.reverse ();//return [3,2,1],arr = [3,2,1] -  to //Sort Array (change the original array) + //sort without parameters, sorted in ascending order; sort with parameters, parameter is a function - vararr = [3,2,6,5,9,1]; theArr.sort ();//return [1,2,3,5,6,9],arr = [1,2,3,5,6,9] * vararr = [9,5,6,8,2]; $Arr.sort (function(A, b) {Panax Notoginseng      returnB-a;//sort in descending order -     //return a-B; Sort in ascending order the});//return [9,8,6,5,2],arr = [9,8,6,5,2] +  A //array to string join is comma delimited by default (does not change the original array) the vararr = [1,2,3,4]; +Arr.join ();//back to "1,2,3,4" -Arr.join (",");//back to "1,2,3,4" $Arr.join ("-");//back to "1-2-3-4"

Iterating through an array

Map ()-Returns a new array in which the elements of the array call the function-processed values of the original array elements (without altering the original array)

Filter ()-Returns a new array of elements in the new array by examining all the elements in the specified array that meet the criteria (without altering the original array)

Some ()-returns TRUE or false, checking each element in the array until it returns true, or False if the full array is checked for false, which executes the specified function only for non-empty elements in the array, no assignment, or the deleted element is ignored

Every ()-returns TRUE or false, checking each element in the array until it returns false, and returns true if the full array is checked for true, which executes the specified function only for non-empty elements in the array, no assignment, or the deleted element is ignored

ForEach ()-iterates through each item in the array

For loop traversal array (not described)

1 //map () = Array.map (function (Value,index,[array]) {},[thisvalue])2 //value--the current array element, index--the current array element, array--the call to the map method, Thiivalue--The This object defined when the callback function is executed (context object)3 //returns the square of each item4 vararr = [A.];5Arr.map (function(val,index) {6      returnVal *Val;7});//return [1,4,9],arr = [+]8 9 //filter () = Array.filter (function (Value,index,[array]) {},[thisvalue])Ten //value--the current array element, index--the current array element, array--call the arrays of the filter method, Thiivalue--The This object defined when the callback function is executed (context object) One //filter out odd numbers A vararr = [1,2,3,4,5,6]; -Arr.filter (function(val,index) { -     returnVal% 2 = = = 0; the});//return [2,4,6],arr = [1,2,3,4,5,6] -  - //some () = Array.some (function (Value,index,[array]) {},[thisvalue]) - //value--the current array element, index--the current array element, array--calls the arrays of the Some method, Thiivalue--The This object defined when the callback function is executed (context object) + //checks if there are elements greater than 5, returns true if none, returns false - vararr = [2,3,4,6]; +Arr.some (function(val,index) { ARetrun val > 5; at});//returns True -  - //every () = Array.every (function (Value,index,[array]) {},[thisvalue]) - //value--the current array element, index--the current array element, array--calls the arrays of the Every method, Thiivalue--The This object defined when the callback function is executed (context object) - //checks if there are elements greater than 5, returns false if none, returns True - vararr = [1,2,3,6]; inArr.some (function(val,index) { -Retrun val > 5; to});//returns false +  - //ForEach () = Array.foreach (function (Value,index,[array]) {},[thisvalue]) the //value--the current array element, index--the current array element, array--calls the arrays of the Foreach method, Thiivalue--The This object defined when the callback function is executed (context object) * //returns each item and index in the array $ vararr = [7,9,5];Panax NotoginsengArr.foreach (function(Val,index,arr) { -Console.log ("index:" + index + ", Value:" +val); the });  + //back to Index:0,value:7 A //Index:1,value:9 the //Index:2,value:5

Interview questions:

1. What is the [1,2,3].map (parseint) return value? Why?

Answer: [1,nan,nan]; because parseint need two parameters parseint (String,[radix]), Radix is the corresponding cardinality of the resolution (radix between 2~36, if Radix omitted or 0, then decimal parsing, When Radix is less than 2 or greater than 36, it returns Nan), and map passes 3 parameters (Value,index,array), the corresponding cardinality is not valid.

This article is only for oneself in peacetime work study to make notes to use! If there are errors please point out!!!

An array of javascript

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.