Introduction and usage of JavaScript Array

Source: Internet
Author: User
Tags javascript array
Array can be considered as a special Object. Pass by value OR by address varv [, 5]; functionf_t (a) {a [1] 9;} f_t (v ); alert (v [1]); // Display 9, the value has changed v [1] 2; f_t (v. slice (); alert (v [1]); //

Array can be considered as a special Object.

By value OR by address)
Var v = [1, 2, 4, 5]; function f_t (a) {a [1] = 9;} f_t (v); alert (v [1]); // Display 9, the value has changed v [1] = 2; f_t (v. slice (); alert (v [1]); // display 2, the value has not changed

The preceding example shows that arrays in JavaScript are transmitted by address.
If you want to pass by value, you must first copy the array, which can be done through the slice () method without parameters.

Array Replication

As mentioned above, the JavaScript Array is assigned a value based on the address. Therefore, if you want to modify an array but do not want to change the value of the original array, You need to copy the array.

The easiest way to copy an array is to create an array and copy it one by one. A convenient method is to use the Array. slice function.

Note that the slice method can only be used for Shallow Copy. For DeppCopy, more encoding is required. See Object Clone in JavaScript)

Var array = [1, 2, 3, 4, 5]; var another_array = array; another_array [2] = 5; alert (array [2]); // because there is no replication, therefore, the original array value var another_array = array. slice (); another_array [3] = 5; alert (array [3]); // After copying, the original array is not affected.
Differences between slice () and splice ()

The arrays of javascrui have two very spelling Methods: slice and splice.

Slice: used for Shallow Copy of the array

Splice: used to modify the array itself and add or modify elements.

Array & Object

TODO

Array Generic Methods

Since arrays are a basic data type, some methods of Array can also be used for some array-like objects.

For example:

Array. prototype. slice. call (nodes );
DOM NodeList 2 Array

How to convert a NodeList to an array if a problem occurs on a network. The following example can be used to explain and answer this question:

Links = document. getElementsByTagName ('A'); links. toString (); // "[object NodeList]" links instanceof Array; // falselinks_array = Array. prototype. slice. call (links); links_array instanceof Array; // true
Array Generic Methods List

Array. forEach (callback, this): iterate each element

Callback (value, index, array)

Array. every (callback, this): return false until the one returnfalse

Array. some (callback, this): return true until the one returntrue

Array. filter (callback, this): create a new Array by filtercallback

Array. map (callback, this): create a new Array by mapcallback

Callback (value, index, array)

Array. reduce (callback, initialValue)

Callback (CED Ced, value, index, array)

Array. reduceRight (callback, initialValue)

Array is actually an Object.
A = ['A', 'B', 'C']; Object. keys (a); // ["0", "1", "2"] a ["1"]; // 'B'

Array is essentially a special Object that is pre-bound with a set of methods. This Object stores the length of an array through the length attribute.

In this way, the following usage is easy to explain.

A = ['A', 'B', 'C']; a [7] = 'G'; a; // ["a", "B ", "c", undefined × 4, "g"]. length-= 2; a; // ["a", "B", "c", undefined × 3]
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.