JS Array learning summary, jsarray Array Summary

Source: Internet
Author: User

JS Array learning summary, jsarray Array Summary

The reference types include Object type (so-called Object), Array type (Array discussed in this article), and Function type.

So what is the array? In my opinion, it is used to save data.

I. Declare an array:

1. constructor var colors = new Array (); in short, new can be omitted, that is, var colors = Array ();

2. array literal var colors = ["black", "green", "pink"];

2. Read and set the value of the array:

Read: colors [x]; parameter x is 0 ~ Colors. length-1;

If this parameter is set, assign a value to colors [x] =, which overwrites the previous value;

Iii. Here I will briefly describe the usage of length:

Colors. length is used to obtain the length of an array. It can be said that there are several items in the array. If there are seven items in an array, but you write colors. length = 2, the next five items will be deleted;

The length attribute can also be used to add data to the end of the array: colors [colors. length] =;

4. Operations in the array:

Method Function Return Value
Array. push (x, y, z) Add xyz to the end of the array Length of the new array
Array. pop () Remove the last entry of the array. Last Deleted item
Array. shift () Remove the first entry of the array. The first item to be removed
Array. unshift (a, B, c) Add a, B, c Length of the new array
Array. reverse () Reverse Array New array after Inversion
Array. sort () Sort the strings of each item in the array in ascending order. Array after sorting
Array. concat (a, B, c) Connection Array Returns the newly connected array.
Array. slice (1, n) Truncate the array, from 1 to n, 1 and n are the index values. Return the truncated array (here, the return value starts from 1 and ends before n)
Array. indexOf (a, start) Locate the location of a, starting from start Returns the index value of a. If no index is found,-1 is returned.
Array. lastIndexOf (a, atart) In contrast to indexOf, lastIndexOf searches from the end Returns the index value of a. If no index is found,-1 is returned.

The splice () method ticket is provided. Why do I obtain the ticket? Because it is awesome;

1. Delete. Two parameters are accepted: the location of the first item to be deleted and the number of items to be deleted;

For example, splice () means to delete the two or three items of the array;

2. insert. Three parameters are accepted: Start position, 0, the item to be inserted.

For example, splice (, "red", "green") inserts red and green at the position where the array index value is 2.

3. replace. Three parameters are accepted: Start position, number of items to be deleted, and items to be inserted.

For example: splice (, "red", "green"), delete the index value of 2, add red and green.

Tips: sort () usage example: function compute (val1, val2) {if (val1 <val2) {return-1;} else if (val1> val2) in ascending order) {return 1;} else {retuen 0} var num = [, 9, 3, 1]; num. sort (compare); alert (num); // 0, 1, 2, 3, 9

5. Iteration Method in array

1. every () and some ():

Query whether each item in the array meets the conditions. If true is returned for each item, result returns true. Var numbers = [0, 1, 2, 3, 4]; var result = numbers. every (function (item, index, array) {return (item> 2)}) alert (result); // false queries whether each item in the array meets the conditions, if either of them returns true, result returns true. Var numbers = [0, 1, 2, 3, 4]; var result = numbers. some (function (item, index, array) {return (item> 2)}) alert (result); // true

2. filter ():

This method returns an array composed of items whose results are true;

3. map ():

var result=numbers.map(function(item,index,array){return item*2;})

Returns the new array after the parameters are executed.

Vi. Merge

Array. reduce ()

var numbers=[1,2,3,4,5];var sum=numbers.reduce(function(prev,cur,index,array){return prev+cur})alert(sum);

In the previous example, reduce () accepts four parameters. The first parameter is the first item of the array, and the second parameter is the second item of the array;

For the first execution of the function, the prev is 1, the cur is 2, the second execution, the prev is 3 (the result of 1 + 2), and the cur is 3.

Array. reduceRight (). Similar to reduce. It only starts from the right side of the array.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.