Javascript-array Array Summary

Source: Internet
Author: User

1. Constructors

    New Array ();     New Array (size);     New Array (Ele0,ele1,ele2,..., Elen);

Parameters

Size: Sets the number of array elements, the length of the returned array is equal to size

2. Properties

Length: equals the ordinal of the last element in the array plus one, and changing the length value cuts or expands the array.

3. Methods

Concat (): Linking elements into arrays

    var a=[1,2,3];    A.concat (4,5);                    // back to [1,2,3,4,5]    A.concat ([4,5]);                // back to [1,2,3,4,5]    A.concat ([4,5],[6,7]);            // back to [1,2,3,4,5,6,7]    A.concat (4,[5,[6,7]);            // return [1,2,3,4,5,[6,7]]

Every (): Tests whether the assertion function is true for each array element

// detects if all elements of an array ages are greater than: var ages = [+, +, +, +]; function Checkadult (age) {    return -age >=;} function myFunction () {    document.getElementById("Demo"). InnerHTML = ages.every (checkadult);}

    The output is: false

Some (): Tests if at least one array element can assert that the function is true, returns a Boolean type, and True/false

    var a = [1,2,3,4,5];    A.some (function(x) {return x%2===0;}) // =>true    A.some (IsNaN)//  =>false

Filter (): Returns an array element that satisfies the assertion function

//Define a callback function.functioncheckifprime (value, index, AR) { high= Math.floor (math.sqrt (value)) + 1;  for(vardiv = 2; Div <= High; div++) {         if(value% div = = 0) {             return false; }     }      return true; } //Create the original array.varnumbers = [31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53]; //Get the prime numbers that is in the original array. varPrimes =Numbers.filter (checkifprime); document.write (primes);//output:31,37,41,43,47,53
    var b = [5,4,3,2,1];     = B.filter (function//[2,1]    everyother = B.filter (function(x,i) { return // [5,3,1]

ForEach (): Iterates through an array of three parameters (array elements, index of array elements, array elements themselves)

    var data = [1,2,3,4,5];     // calculating elements and values    var sum = 0;    Data.foreach (function//sum =    +// value of each array element plus 1     Data.foreach (function//data = [2,3,4,5,6]

IndexOf ()/lastindexof (): Find the index of the matching element in the array subscript 2 parameters (search value, start index subscript); No function can be found, return 1

 //  insert all occurrences of x in the array and return an array containing the matching index  Span style= "color: #0000ff;" >function  finally   (a,x) { var  results = [], // len = A.length,//
     pos = 0;    // while         (POS < = A.indexof (X,pos);  if  (pos = = =-1) break  ;        Results.push (POS);    POS  = pos +1;   return   results; }

Join (): Converts all elements of an array into strings and joins (is the inverse of Split ())

  var a = [n/a];   A.join ();     // = "  the" A.join ("");    // = "1 2 3"  A.join ("");    // = "123"  var New Array (ten);  B.join ("-");    // + "---------": 9 Consecutive strings

Split (): Splits a string into blocks to create an array parameter (splits based on the character, the number of reserved bits of the split array)

    "2:3:4:5". Split (":")    // will return ["2", "3", "4", "5"]    "|a|b|c". Split ("|")    // will return ["", "a", "B", "C"]    // To divide a word into letters, or to divide a string into characters, use the following code:    " Hello ". Split (" ")    // can return [" H "," E "," L "," L "," O "]    // if only a subset of the characters need to be returned, use Howmany parameter:    "Hello". Split ("", 3)    // can return ["H", "E", "L"]

Map (): Passes each element of the called array to the specified function and returns an array containing the function's return value parameter (function (element of the array))

    a[1,2,3];     = A.map (function(x) {        return x*x;    });

Pop (): Deletes and returns the last element of the array, reducing the length of the array

Shift (): Removes the first element from the array and returns the value of the first element. Decrease Array Length

var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.shift () console.log (fruits); // results: Banana, Orange, Apple

Unshift (): Inserts an element at the head of the array and moves the existing element back to the parameter (value,..., N) returns the value: The new length of the array

    var a = [];    A.unshift (1); // =>a:[1]        return: 1    a.unshift (+);   =>a:[22,1]        return: 2    a.shift ();   =>a:[1]        return:    a.unshift (33,[4,5]);   =>a:[33,[4,5],1]        return: 3

  Push (): Adds one or more solemnity to the end of the array and returns the new length of the array

    var stack = [];    (Stack.push);    // stack: [Up]    Stack.pop ();        // stack: [1]    Stack.push (3);        // stack: [1,3]    Stack.pop ();        // stack: [1]    Stack.push ([4,5]);    // stack: [1,[4,5]]    Stack.pop ();        // stack: [1]    Stack.pop ();        // stack: []

Reduce (): Calculates a value parameter from the elements of the array (contains the return value function, initial value [temporary use, first use of initial value, two times later using the first parameter's return values replaced])

Reduceright (): ditto, but in reverse order, the array is processed from right to left

    var a = [1,2,3,4,5];     var sum = a.reduce (function(x, y) {return x+y},0); // Array summation    var produce = A.reduce (function(x, y) {return x*y},1); // Array quadrature    var max = A.reduce (function(x, y) {return x>y?x:y}); // To find the maximum value

Reverse (): Reverses the order of array elements in the original array

    var a = [N/a    ]; // = = 3,2,1 current array a=[3,2,1]

Sort (): Sorts the array and returns the sorted array, which is alphabetical by default when you call sort () without parameters

    var New Array ("Banana", "cherry", "apple");    A.sort ();     var s = A.join (","); // =>s = = "Apple,banana,cherry"

  Slice (): Returns part of the array parameter (start position, end position [without end position]) If there is only one argument, the negative number of all elements starting from the position of the argument to the end of the array is the reciprocal, for example, 1 is the last, and 3 is the third last one.

    var a = [1,2,3,4,5];    A.slice (0,3); // =>[1,2,3]    A.slice (3);    // =>[4,5]    A.slice (1,-1); // =>[2,3,4]    A.slice ( -3,-2); // =>[3]

Splice (): Insert, delete, or replace an array element parameter (specify the starting position of the insertion or deletion, specify the number of elements to delete from the array, or delete from the beginning to the end if omitted) returns an array of deleted elements, returning an empty array without deleting

    var a = [1,2,3,4,5,6,7,8];    A.splice (4); // = = Returns [5,6,7,8];a is [1,2,3,4]    (A.splice); // = = Returns [2,3];a is [1,4]    A.splice (a); // = = return [4];a Yes [1]

      The first two parameters specify the array elements that need to be deleted, and the third n multiple parameters specify the elements that need to be inserted into the array, starting at the position specified by the first parameter.

    var a = [1,2,3,4,5];    A.splice (2,0, ' A ', ' B '); // = = Returns [];a is [, ' A ', ' B ', 3,4,5]    A.splice (2,2,[1,2],3); // = = Returns [' A ', ' B ']; A is [1,2,[1,2],3,3,4,5]

toLocaleString (): Converting an array to a localized string

ToString (): Converts an array to a string

  

Javascript-array Array Summary

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.