JS learning the text of an array

Source: Internet
Author: User

An array of JavaScript can contain various types of data.

1. The length of the array, directly with lengthProperties
var arr=[1,2,3];arr.length;

In JS, assigning the length of an array directly will change the size of the array, for example:

var array=[1,2,3];console.log (array.length); // the output array has a length of 3array.length=5; // Assigns a value of 5 to the length of the array, at which point the contents of the array become [1,2,3,undefined,undefined]console.log (array); // console.log output will still output a few, but the length of the array has changed, if you change to alert (array), you can see the popup box content ofConsole.log (Array[4]); // output is undefined
2. Assignment and modification values of arrays

The array can be modified by the index value within the values such as and assignment

var array=[1,2,3];array[0]=0; console.log (array); // output is 0,2,3

!!! If an index is assigned a value, the index exceeds the length range of the array, causing the array size to change:

var array=[1,2,3];array[5]=5; console.log (array); // array becomes [1,2,3,undefined,undefined,5]

Generally it is better not to directly change the size of the array, easy to cause problems.

3. IndexOf ()

Arrays can be viewed by indexof () to see the position of an element

var arr=["A", "B", "C"];console.log (Arr.indexof ("C")); // The output result is 2console.log (Arr.indexof (2)); // The output is-1, and when Arr does not contain this value for the query, it returns-1
4. Push () and pop ()

Push () adds several elements to the end of the array, and pop () removes the last element of the array.

Push () Example:

var arr=["A", "B", "C"];arr.push ("Sky", "car"); Console.log (arr); // arr content changes to ["A", "B", "C", "Sky", "car"]

Pop () example, then the push () example continues:

Console.log (Arr.pop ()); // output car; A method is found that can take advantage of pop () to get the last element of the array console.log (arr); // Arr's content becomes ["A", "B", "C", "Sky"], with four values remaining Arr.pop (); Arr.pop (); Arr.pop (); Arr.pop () ; // arr four consecutive pops, arr becomes empty array Console.log (Arr.pop ()); // empty array continuation pop does not error, but returns undefined value

!!! A method is found that can take advantage of pop () to get the last element of an array

5. Unshift () and Shift ()

Unshift () Inserts a number of elements into the beginning of the array, and shift () is the first element of the Lightning array.

Unshift () Example:

var arrshift=["A", "B", "C"];arrshift.unshift ("Sky", "car"); Console.log (arrshift); // Arrshift changed to ["Sky", "car", "a", "B", "C"]

Shift () Example:

Console.log (Arrshift.shift ()); // Output Skyconsole.log (Arrshift); // Arrshift changed to ["Car", "a", "B", "C"] Arrshift.shift (); Arrshift.shift (); Arrshift.shift (); Arrshift.shift () ; // shift () four times, the array becomes an empty array Console.log (Arrshift.shift ()); // empty Array shift () will not error, return undefined value

!!! Similarly, you can use Shift () to get the first element of an array

6. Join ()

The join () method is used to concatenate each element of the array with the specified string, and then return the concatenated string, which is a very useful method. Example:

var arrjoin=["Olive", "Kong"];console.log (Arrjoin.join ("-")); // returns the Olive-kong string

If a string element is not stored in the array, the join () method automatically converts the element to a string and then joins it. Example:

var arrjoin=[13,14];console.log (Arrjoin.join ("")); // returns 1314 String
7. Reverse ()

The reverse () method is to invert the array. Example:

var arrreverse=["Kong", "Olive"];console.log (Arrreverse.reverse ()); // Arrreverse became ["Olive", "Kong"]
8. Sort ()

The sort () method is used to sort the array, and when called directly, it is sorted in the default order. Example:

vararrsort=[2,5,1,40,26];console.log (Arrsort.sort ());//sorted by [1, 2, +, 5]vararrsort2=["Sort", "block", "CSS", "html", "JS", "Sky"];console.log (Arrsort2.sort ());//sort results for ["Block", "CSS", "html", "JS", "Sky", "sort"]varArrsort3=[1, "block", "CSS", "html", "JS", "Sky"];console.log (Arrsort3.sort ());//sort results for [1, "Block", "CSS", "html", "JS", "Sky"]varArrsort4=[1, "2", "3", "HTML", "JS", "Sky"];console.log (Arrsort4.sort ());//sort results for [1, "2", "3", "HTML", "JS", "Sky"]

You can see that the default sort order is that the numbers are preceded, the characters are followed, the strings are sorted alphabetically, the numbers are not sorted by size, and the numbers can be sorted in ascending or descending order. Of course, please see the article "JS Learning--arrays in descending order of ascending", here is not much to do introduction.

9. Slice ()

The slice () method is used to intercept some elements of an array and then return a new array.

var arrslice=[1,2,3,4,5,6];console.log (Arrslice.slice (0,4)); // intercepts the element from index 0 to index 4 (excluding the value indexed to 4), the result is [1,2,3,4]Console.log (Arrslice.slice (4)); // intercepts all elements from index 4, including elements indexed to 4, with the result [5,6]
Splice ()

The splice () method is used to modify the array (which can be added or deleted), and it can delete several elements from the specified index, and then add several elements from that location. Example:

var arrsplice=["Olive", "Kong", "is", "studying", "JS"];console.log (Arrsplice.splice (3,2, "playing "," outside ")); // Delete 2 elements starting at index 3 and then add two elements: return value ["Studying", "JS"]Console.log (arrsplice); // after the above statement, Arrsplice becomes ["Olive", "Kong", "is", "playing", "outside"]

Only delete without adding, example (followed by the above example):

Arrsplice.splice (0,2); // Delete Two elements backward from index 0, without adding any elements console.log (arrsplice); // The array becomes ["is", "playing", "outside"]

Just add not Delete, example (continue with the example above):

Arrsplice.splice (0,0, "Popeye"); // starting at index 0, 0 elements are deleted backwards, then 1 elements are added, and the return value of console is []console.log (arrsplice); // The array becomes ["Popeye", "is", "playing", "outside"]
Concat ()

The Concat () method is used to concatenate the current array with another array and return a new array. Example:

var arrconcat=[1,2,3]; var arrnew=arrconcat.concat (["A", "B"]); Console.log (arrconcat); // The return value is [Console.log], the original array is not changed (arrnew); // returns a new array of [1, 2, 3, "A", "B"] // concat can connect several arrays var arrC1 = ["George", "John", "Thomas"]; var arrC2 = ["James", "Adrew", "Martin"]; var arrC3 = ["William", "Franklin", "Martin"];console.log (Arrc1.concat (ARRC2,ARRC3));

JS learning the text of an array

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.