An array of data structures

Source: Internet
Author: User


I was in Guangzhou Physical Education Institute of a sports student, and later through some coincidence contact the front end, think very interesting. A line of code can write a cool Web page and complete the awesome user interaction. So decided to step into the field of it and conduct a brutal hair-off practice. It has been studying for half a year to go for a spin. Today this is the first blog I wrote, I intend to record some of my experience in the trip, but also to make a summary, good nonsense not much to say, began to write it.

Arrays are data types that are frequently encountered in JS. Arrays can store the same series of data, or they can store values of different data types in an array.

First summarize some of the core methods of the array: {

1, concat stitching two or more arrays, and return the results

2. Join joins all elements of the array into strings

3, INDEXOF returns the first index with the given parameter, no returns-1

4, LASTINDEXOF returns the last index with the given parameter, no return-1

5. Sort the array elements

6. ToString returns the array as a string

7. ValueOf returns the array as a string

Wait a minute

}


Create an array var arr=[];
var arr1=new Array ();
var arr2=new array (5);//Create an array of the specified length

You can also use elements to initialize an array: var arr3=[1,2,3,4,5,6,7];//uses this array as an example below

If you want to know that there are several elements in an array, you can use the length property of the array. such as the above arr3.length//7

If you want to access elements in a particular position in an array, you can use the array brackets to add an element's subscript, such as the arr3[0]//result is 1. Array subscript is calculated starting from 0

To output all the elements of an array, we can iterate over the array

var num= '//Declare an empty variable

for (Var i=0;i<arr3.length;i++) {

Num+=arr3[i]

}

Console.log (num)//All elements of the output array

Add and delete elements of an array:

1. Add element: Trailing element push (), header add Unshift (), and a specified location add splice (INDEX,REMOVELENGTH,ITEM1......,ITEMN),

Adding elements can also be assigned directly to an array, such as arr3[8]=2, which equals the number 2 added to the end of the array.

2. Delete element: Tail Delete and return the element pop (), head delete element shift (), specify location delete Splice (index,removelength)

The splice () method inside the parameter index, Removeindex is required. Index is the specified element position, Removelength is the number of deleted elements (if fill 0 is not deleted), item1--itemn is the added element, can have n values.

3, array Merge: Concat () method: arr3=[1,2,3,4,5,6,7];

NEWARR=[8,9];

Arr3.concat (Newarr)//merge two arrays, output ARR3=[1,2,3,4,5,6,7,8,9]

4, Join method: The array is stitched into a string form returned. Arr3.join ('-')//will return "1-2-3-4-5-6-7"

5. Sort the array: Given an unordered array of narr=[1,3,5,7,3,2,6,2]; we can sort directly with narr.sort () no problem. Results Output is//[1, 2, 2, 3, 3, 5, 6, 7]

But if it is narr1=[1,3,21,33,52,6] use Narr1.sort () to sort the output results are//[1, 21, 3, 33, 52, 6]

Why this happens because the sort method compares elements by default to strings when comparing

So since the incoming is all numbers, we can give him a method, so write Narr1.sort (a, b) {

Return A-B

})//This will be compared when a/b is passed in the sort, and if a>b returns a positive number, the inverse returns a negative number and returns 0 as equal. The sort method sorts according to the value returned.

If you don't understand then we can write function Arrsort (A, b) {

if (a>b) {

Return 1}else if (a<b) {

return-1}else{

return 0}

}

The Narr1.sort (Arrsort)//sort method invokes the Arrsort () function defined earlier and then uses it to sort the array.

6, search: There are two methods, respectively, are indexof and LastIndexOf. Here we redefine the array var arr=[1,2,3,4,5,7,1]; Then we use two methods to search the array for the number 1, to see the output

Arr.indexof (1)//The return index is 0, and the method returns the first index that matches a given element

Arr.lastindexof (1) The return index is 6, and the method returns the last index that matches the given element

7, there are foreach, slice, every and other methods are not introduced, will be written after

Finally write a small example: it is known that the first number of Fibonacci sequence is 1, the second number is 2, starting from the third item, each item equals the first two and the first 20 digits of the sequence.

var num=[];

Num[1]=1;

num[2]=2;

for (Var i=3;i<20;i++) {

NUM[I]=NUM[I-2]+NUM[I-1]

}

for (Var i=0;i<num.length;i++) {

Console.log (Num[i])

}

An array of data structures

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.