JavaScript Basics (Arrays)

Source: Internet
Author: User

+ , Array

    • Defining arrays

* Literal Way

var Array name = [Value,value,...]

* Constructor Function mode

var Array name = new Array (value,value,...) ; var Array name = new Array (length)

Create an object way to create an array analysis diagram

Attached: var num1 = 100; Number var num2 = new number (100); Object

var str1 = ' string '; string var str2 = new String (' string '); Object

var boo1 = true; boolean var Boo2 = new Boolean (true); Object

var arr1 = [1,2,3,4]; Object var arr2 = new Array (1,2,3,4); Object

Array classification

2 indexed arrays-arrays created by default

(1) Create an empty array (2) Create an indexed array based on an empty array

format: var Array name  = new Array (); Array name [ index value ] = value;

" index value is number (starting from 0)"

Note: When no value is stored in a specific location-undefined

2 Associative arrays-JavaScript-like objects

format: var Array name  = new Array (); Array name [ index value ] = value;" index value is string"

Note: The concept of associative arrays is not available in the ECMAScript official specification

Sparse array

Concept: The length of an array is greater than the number of elements in an array where the element has no value, the default undefined

The properties of the U array-length

Non-sparse array: The value of the length property = The current array contains the number of elements.

Sparse array: The value of the Length property > The current array contains the number of elements.

Eg:var arr2 = [];  ARR2[14] = ' a '; Console.log (arr2.length);//Output 15

Actions (Get/modify/delete)

2 getting the elements in an array: array name [ index value ]

2 modifying elements in an array: array name [ index value ] = new value

2 Deleting an element in an array : Delete array name [ index value ]

Note: Only the value at the corresponding location is deleted, but the position is reserved-undefined

U traversal Array

* for statement: Traversal array can control the start and end

* for.in statement: Only iterate through the array from start to finish

U Variables and Arrays

Analytic diagram of assignment operation of variables

An analytic diagram of an assignment operation for an array

U two-dimensional array

Concept: Nested structure traversal of arrays: for nested statements

L Common methods of operation

    • Detection data type
    • Conversion Method ToString ()
    • In-and-out stacks-add/remove have an effect on the original array, delete returns the element added returns the length "

push () -Adds the last element to the array pop () -Deletes the last element in the array

Unshfit () -Adds an element to the beginning of the array shfit () -Deletes the first element in the array

    • Sorting methods

reverse () -reverses the order of the elements in the array

Sort () -sorts the elements of an array

Note: The default sort is not sorted by small to large. If the number is two digits, it is arranged in the first digit.

Method: Use the array name. Sort () inside the constructor to change the default ordering so that it is ordered sequentially.

Eg:arr.sort (function (b) {

return a < b;  ------< from large to small; > From small to large

});

    • Interception method:

2 Slice (start,end) "Note: There is no effect on the original array"

The start---Indicates which index value from the array begins to intercept; end---By default, indicates the end of the array

Set Value: Represents the previous return value intercepted to the current index value: The result of the interception

var arr1 = Arr.slice (1,3); Console.log (ARR,ARR1);

2 Splice (index, Howmany, Element1,element2,... elementn) "NOTE: default is an array"

Index---represents the indexed value of the current array (where it was deleted or added)

Howmany---Indicates the number of deleted elements (if 0, does not delete) Element1---represents the added element

* Note-is added to the index position of the array; return value-represents the currently deleted element

    • Search Content--Location

2 indexOf (Element)

Element-represents the content of elements searched in an array

Usage: Starts the search from the index value of 0 in the array, then backwards; return value: The index value of the first matched element

Note: If the searched element does not exist in the array, 1 is returned

2 lastIndexOf (Element)

Element-represents the content of elements searched in an array

Usage: Return value: The index value of the last matching element

JavaScript Basics (Arrays)

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.