Summary of Array array learning in JS

黄舟
Release: 2017-02-10 10:04:21
Original
938 people have browsed it

First time writing a blog. . . A bit square. . .

I am a novice (a real novice), self-study of front-end. Now I will talk about some array operations that I summarized during the learning process. If there are any mistakes, please point them out. Humbly accept.

Reference types are divided into Object types (so-called objects), Array types (arrays discussed in this article), Function types, etc.

So, what does an array do? It seems to me that it is used to save data.

1. Declare an array:

1. Constructor var colors=new Array(); for abbreviation, you can omit new, 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 Here is a brief talk about the usage of length:

colors.length gets the length of the array, which can also be said to be the number of items in the array. If an array has 7 items, but you write colors.length=2, Then the next 5 items will be deleted;

Using the length attribute, you can also add data to the array at the end: colors[colors.length]=for assignment;

4. Operations in the array:

Method Function Add xyz to the end of the array ##Array.pop() Remove the end of the array One item Remove the last item Array.shift() Remove the first item of the array Shift The first item to be divided Array.unshift(a,b,c) Add a,b,c at the front of the array New array length Array.reverse() Reverse array The new array after reversal Array.sort() Arrange the strings of each item in the array in ascending order Reordered array Array.concat( a,b,c) Connect arrays Return the connected new array Array.slice(1,n) Intercept the array, from 1 to n, 1 and n are the index values Return the intercepted array (return here starts from 1 and ends before n) Array.indexOf(a, start) Find the location of a, starting from start Return the index value of a, if not found, return -1 Array.lastIndexOf(a,atart) Contrary to indexOf, lastIndexOf searches from the end Returns the index value where a is located, if it is not found, it returns -1
Return value Array.push(x,y, z)
New array length

The splice() method is listed here. Why take it out alone? Because it’s awesome;

1. Delete. Accepts two parameters: the position of the first item to be deleted and the number of items to be deleted;

Example: splice(1,2), which is to delete items 2 and 3 of the array;

2. Insert. Accepts three parameters: the starting position, 0, and the item to insert.

Example: splice(2,0,"red","green") will insert red and green at the array index value 2.

3. Replacement. Accepts three parameters: starting position, number of items to delete, and items to insert.

Example: splice(2,1,"red","green"), delete the item with index value 2, and add red and green.

(val1< -1 (val1> 10
Copy after login


Five. Iteration methods in arrays

1.every() and some():

numbers=[0,1,2,3,4 result=numbers.every( (item>2 numbers=[0,1,2,3,4 result=numbers.some( (item>2
Copy after login


2.filter( ):

This method will return an array composed of items whose result is true;

3.map():

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

Returns the new array after the array executes the parameters.

six. merge.

Array.reduce()

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


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

The first time the function is executed, prev is 1, cur is 2, the second time it is executed, prev is 3 (The result of 1+2), cur is 3.

Array.reduceRight(). Similar to reduce. Just start from the right side of the array.

The above is the summary of Array array learning in Js. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!