JS Array Object

Source: Internet
Author: User
Tags terminates

Array.join (para)

Converts all elements in an array into strings and joins together, para the delimiter between elements after merging

Array.reverse ()

Reverses the order of the elements in the array, returning an array of reverse orders

Array.Sort ()

Array elements are sorted in alphabetical order without parameters, and a comparison function must be passed to the sort method if it is ordered in a different way than the word target order.

var a = [22,4,1111,222];
A.sort () //[1111,22,222,4]
A.sort (function (A, b) { //[4,22,222,1111]
return a-B;
});
  
A.sort (function (A, b) { //[1111,222,22,4]
return b-a;
})

Array.concat ()

The method creates and returns an array

var a = [22,4,1111,222];
A.concat (4,5); Back to [22,4,1111,222,4,5]
A.concat ([4,5]); Back to [22,4,1111,222,4,5]
A.concat ([4,[5,6]]); return [22,4,1111,222,4,[5,6]]

Array.splice (Index,length,insert)

Length is greater than 0 o'clock, deleting data from index, length, and inserting insert after index

Array.isarray (Element)

This is a static function of the array object that is used to determine whether an object is not an array

var a = new Array (123);        var B = new Date ();        Console.log (Array.isarray (a)); True        Console.log (Array.isarray (b));//false
. IndexOf (Element)/. LastIndexOf (Element)

As the name implies, these two methods are used to find the position of the specified element within the array, find the first one to return its index, and not find the return -1,indexof to search from beginning to finish, LastIndexOf reverse search.

var a=new Array (1,2,3,3,2,1);           Console.log (A.indexof (2)); 1        console.log (A.lastindexof (2));//4
. ForEach (Element,index,array)

Iterate through the array, the parameter is a callback function, the callback function has three parameters: the current element, the element index, the entire array

var a=new Array (1,2,3,4,5,6);        A.foreach (function (e,i,array) {            array[i]=e+1;            });           Console.log (a); [2, 3, 4, 5, 6, 7]
. Every (function (Element,index,array))/. Some (function (Element,index,array))

These two functions are similar to the logical decision in discrete mathematics, and the callback function returns a Boolean value that returns True when each callback function of the "All" function returns true and terminates execution when the every is encountered, returning the False;some function as "exists" A callback function returns True when execution terminates and returns true, otherwise false is returned. Calling every on an empty array returns True,some returns FALSE.

var a=new Array (1,2,3,4,5,6);        /*0:1         1:2        2:3        3:4         4:5         false */Console.log (A.every (        function (E,i,arr) {            Console.log (i + ': ' +e);            return e<5;            });
var a=new Array (1,2,3,4,5,6);        /*0:1         1:2        2:3        3:4         4:5         true */        Console.log (A.some (function (e,i,arr) {            console. Log (i+ ': ' +e);            return e>4;            });
. map (function (Element))

Similar to foreach, iterates through an array, returns a new array of callback functions, returns a new array with the same structure as the original array, and the original array unchanged

var a=new Array (1,2,3,4,5,6);        Console.log (A.map (function (e) {            return e*e;            }));//[1, 4, 9, C, D, d]         Console.log (a);//[1, 2, 3, 4, 5, 6]
. filter (function (Element))

Returns a subset of the array in which the callback function is used to determine whether the logical return is returned, true to add the current element to the returned array, false to not add, the new array contains only values that return True, the index is missing, the original array remains unchanged

var a=new Array (1,2,3,4,5,6);        Console.log (A.filter (function (e) {            return e%2==0;            }));//[2, 4, 6]         Console.log (a);//[1, 2, 3, 4, 5, 6]
. reduce (function (V1,V2), value)/. Reduceright (function (V1,V2), value)

Iterate through the array, call the callback function, combine the elements of the array into a single value, reduce the index from the minimum value, reduceright the reverse, the method has two parameters

1. Callback function: The two values are combined into one, return the result

2.value, an initial value, optional

var a=new Array (1,2,3,4,5,6);        Console.log (A.reduce (function (v1,v2) {            return v1+v2;            }));//            Console.log (a.reduceright (function (v1, V2) {                return v1-v2;                },100)); 79

JS Array Object

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.