JavaScript array-Method Summary of array (recommended) _javascript tips

Source: Internet
Author: User
Tags prev javascript array

The array type in JavaScript is often used, and the array type provides a number of ways to achieve our needs, and here's a summary

First, the way to create an array

var colors=new Array ();

var colors=new array (3);//Create an array of length 3

var colors=new array ("Red", "blue")//Create an array ["Red", "blue"]

Of course, the above new can be omitted not to write, such as Var Colors=array ("Red");

2, directly using the literal amount of the array

var colors=["Red", "Blue", "green"];

Two, array Method 1

var colors=["Red", "Blue", "green"];

1, get the length of the array COLORS.LENGTH;//3

2, access to array of the second item Colors[1];//blue

3, change the second item of data colors[1]= "black";//["Red", "Black", "green"

4, check whether it is an array colors instanceof array;//true

5, colors.tostring ();//By default, the output string is separated by commas Red,blue,green

6, Colors.join ("|"); /Custom with "|" Separate output string Red|blue|green

7, Colors.push ("brown")//Add an item to the tail of the array

8, Colors.pop ()//To the end of the array to delete an item

9, Colors.shift ()//delete the first item of the array, and get the value

10, Colors.unshift ("K1", "K2")//to the array before inserting these two; ["K1", "K2", "Red", "Blue", "green"];

11, Colors.reverse ()//Flip The order of the array

12, Colors.sort () or Colors.sort ([func]);

13, Concat () returns a new array, does not affect the original array colors.concat () or Colors.concat ("K1");

14. Slice (begin,end) copies this data from the array subscript begin to end, excluding the subscript end, if it is slice (begin), from the subscript begin to the tail of the array

15, Splice

Splice (0,2)//Delete two items of an array starting with subscript 0

Splice (2,0, "K1", "K2") deletes 0 items starting with subscript 2, followed by inserting two

Splice (2,1, "K1")//starts with subscript 2 to delete an item, then inserts an item from here

16, IndexOf ("item")//from the array header to find an item, find a return subscript value, can not find the return-1

17, LastIndexOf ("item")//from the end of the array to find an item, found after the return of the subscript value, can not find the return-1

Three, array Method 2: Iterative method (ECMASCRIPT5)

1, every (): Every item in an array runs the given function, each entry returns True, and returns true (without affecting the original array)

var numbers=[1,2,3,2,1];
Determine if each number is greater than 2
var flag=numbers.every (function (item,index,array) {return
  item>2;
});

2, Filter (): Each item in the array runs the given function, and returns the item with the function true (without affecting the original array)

var numbers=[1,2,3,2,1];
Returns the item
var array=numbers.filter (function (item,index,array) {return
  item>2;
}) that is greater than 2;

3, ForEach (): Each item in the array executes the given function, does not return a value (does not affect the original array)

var numbers=[1,2,3,2,1];
Outputs the square numbers.foreach of each item
(function (item,index,array) {
  console.log (item*2);
});

4, Map (): Each item of an array executes a given function, returning an array of results after each function call (without affecting the original array)

var numbers=[1,2,3,2,1];
Returns the Square
var array=numbers.map (function (item,index,array) {return
  item*item;
}) of each item;

5, some (): Each item of the array executes the given function, and returns True if one of the entries returns true

var numbers=[1,2,3,2,1];
var flag=numbers.some (function (item,index,array) {return
  item>2
});

Three, array Method 3: Merge Method (ECMASCRIPT5)

1. The reduce () method begins with the first row of the array, traversing to the last

2. The Reduceright () method begins with the last item of the array and traverses forward

var numbers=[1,2,3,4,5];
var result=numbers.reduce (function (prev,cur,index,array) {
  //prev: The result of the previous operation, the first entry for the number at first
  //cur: The current item of the array
  //index: The subscript//array of the current array
  : The array that executes the operation, currently numbers
  Console.log ("prev:" +prev);
  Console.log ("cur:" +cur);
  Console.log ("index:" +index);
  Console.log ("array:" +array);
  Console.log ("=============");
  return prev+cur;
});

The above JavaScript arrays-array method summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.