Array of getting started with javascript [For Beginners]

Source: Internet
Author: User
Array of getting started with javascript [For Beginners] 1. define arrays.

There are two methods to define an array:

1. var arr1 = []; // defines an empty array.

2. var arr2 = [1, 2, 3, "str1", "str2"]; // defines an array with five elements.

3. var arr3 = new Array (3); // defines an empty Array.

4. var arr4 = new Array (1, 2, 3, "str1", "str2"); // defines an Array with a specified length of 5.

2. Read and Write of array elements.

Arr [0]; // read the first array element

Arr [0] = "str1"; // change the value of the first element of the array.

3. sparse array.

Sparse array indicates an array with non-consecutive indexes starting from 0. Generally, the length of an array indicates the number of elements in the element. If the array is sparse, The length attribute value is greater than the number of elements.

The in operator is used to check whether an element exists at a certain position. Note that undefined also exists.

For example, var a1 = [,];

Var a2 = new Array (3 );

0 in a1; // true, because a [0] has an undefined Element

0 in a2; // false, a2 has no element at index 0

Iv. array Length

The length attribute is used to indicate the length of the array.

For example, var arr = [1, 2, 3, 4, 5];

Arr. length; // 5 the arr array has 5 elements.

5. add and delete array elements

Push: // Add an element at the end of the array

Var arr = [1, 2, 3];

Arr. push (); // arr is changed to [, 5]

Delete: // delete an element at a certain position in the array

Var arr = [1, 2, 3]

Delete arr [1] // change arr to [1, 3]

1 in arr // false

6. array Traversal

The for statement is usually used for Array traversal.

Var arr = [1, 2, 4, 5];

For (var I = 0. I

If (! A [I]) continue; // skip null, undefined, and nonexistent Elements

}

VII. multidimensional array

Multi-dimensional arrays are elements in arrays or arrays.

For example, var arr = [[, 3], [, 6];

Arr [1] [1]; // 5

8. Array Method

1. join () is used to convert all elements in the array into strings and connect them together. You can also customize the connection characters.

Var arr = [1, 2, 3];

Arr. join (); // => "1, 2, 3"

Arr. join ("="); // => "1 = 2 = 3 ";

2. reverse () is used to reverse the order of array elements

Var arr = [1, 2, 3];

Arr. reverse (); // The arr array is changed to [3, 2, 1].

3. sort (); // sorts the elements in the logarithm group. You can input a function for sorting. If it is null, It is sorted alphabetically. Undifined element to the end

Var arr = [1, 2, 3];

A. sort (function (a, B ){

Return a-B; // The positive number of the sorting standard negative value 0. The smaller value is returned first.

}); // The value of the arr array is [1, 2, 3]. If the second condition changes to B-a, the result is [3, 2, 1].

4. concat () // combines a new array and returns a new array.

Var arr = [1, 2, 3]

Arrnew = arr. concat () // The arrnew array is [, 5]

Arrnew1 = arr. concat ([], []); // the array of arrnew1 is [, 7].

5. slice () // returns an array composed of elements in the specified range of the array. If you enter a parameter, it is an array from this parameter to the end. The first parameter is the starting position, and the second parameter is the number.

Var arr = [1, 2, 4, 5];

Var arr1 = arr. slice (2); // [3, 4, 5]

Var arr2 = arr. slice (1, 3); // [2, 3]

6. splice () deletes or adds elements. The original array is changed, which is equivalent to the reference (ref) in C #. The original array is an array composed of the deleted elements, and the returned value is an array composed of the remaining elements.

Var arr = [1, 2, 4, 5];

Var arr1 = arr. splice (); // arr is [2, 3, 4], and the returned array arr1 is []

Var arr2 = [1, 2, 4, 5];

Var arr3 = arr2.splice (2nd, 'A', 'B'); // Delete two elements from the bits, and insert 'A' from the position ', 'B'; arr2 is [] because no elements are deleted, arr3 [, 'A', 'B', 3,4, 5]

7. add or delete an element at the end of the array by push () and pop (). The last added element is returned and deleted. The returned value is the deleted element.

The push () function adds an element to the end of the array.

The pop () function deletes the last element of the array.

Var arr = [1, 2, 3]

Arr. push (4); // arr is [1, 2, 4]

Var arr1 = [1, 2, 3]

Arr. pop (); // arr1 is [1, 2]

8. unshift () and shift ()

Shift (), unshift (), push (), pop () are only operations in the array header rather than the tail.

Shift () removes an element from the array header. The returned value is the deleted element.

Unshift () adds an element to the array header, and the return group is the last added element.

Var arr = [1, 2, 3];

Var a = arr. shift (); // arr is changed to [2, 3] a to 1

Var arr1 = [1, 2, 3];

Var B = arr1.unshift ([]); // arr1 is changed to [,]. If B is 4, the last one is returned. Add 5 and then add 4.

9. toString () and toLocaleString () convert the array to a string.

Var arr = [1, 2, 3]

Arr. toString (); // generate ", 3" is the same as join () without any parameters.

Ii. array method in ECMAScript

1. forEach () traverses the array from start to end and calls the specified function for each element.

Var arr = [1, 2, 3, 4, 5];
Var sum = 0;
Arr. forEach (function (value ){
Sum = sum + value;
});
Document. write (sum); // sum is eventually 15

2. The map () method passes each element of the called array to the specified function and returns an array.

Var arr = [1, 2, 3, 4, 5];
Var arr1 = arr. map (function (value ){
Return value + 1;
});
Document. write (arr1.join (); // arr1 is [2, 3, 4, 5, 6]

3. filter (): The returned element is a subset of the called array to filter out non-conforming elements.

Var arr = [1, 2, 3, 4, 5, 6];
Var arr1 = arr. filter (function (I) {return I % 2 = 0 });
Document. write (arr1.join (); // The value of arr1 is [2, 4, 6].

4. every () and some ()

Every () returns true only when all elements in the array call the function returns true. If the first return is false, the traversal is stopped.

Some () returns true if an element in the array calls the function to return true. If the first return is true, the traversal is stopped.

Var arr = [1, 2, 3, 4, 5, 6];
Var a = arr. every (function (x) {return x> 3 ;});
Var B = arr. some (function (y) {return y> 3 ;});
Document. write ("the value of a is:" + a); // The value of a is false. Not all elements in a are greater than 3.
Document. write ("B's value is:" + B); // B's value is true, and B has more than 3 elements.

5. reduce () and reduceRight ()

Reduce () combines elements in the array with specified functions to generate a single value. The first parameter is the simplified operation function, and the second parameter is the initial value passed to the function. The final result is that the initial value is calculated based on the combined function and the final result. The second parameter indicates that the initial value can be omitted. If the initial value is omitted, the calculation starts from the first element.

Var arr = [1, 2, 3, 4, 5, 6];
Var count = arr. reduce (function (x, y) {return x + y ;}, 0 );
Document. write (count );
ReduceRight (); the only difference from reduce () is that it selects elements from right to left for calculation.

6. indexOf () and lastInsexOf ()

IndexOf () returns the index of the first element found at least from the beginning and end.

LastIndexOf () searches for elements in reverse order and returns the index of the first element to be found.

Var arr = [1, 2, 3, 2, 1];
Var I = arr. indexOf (2); // search from the beginning to the end. The first occurrence of 2 is arr [1], so 1 is returned.
Var j = arr. lastIndexOf (2); // searches from the end to the header. The first time we encounter 2, it is arr [3]. Therefore, 3 is returned.
Document. write (I +"
");
Document. write (j );

7. Array. isArray (); // determines whether an object is an Array object.

Var arr = [1, 2, 3];
Var str = "str1 ";
Document. write (Array. isArray (arr); // returns true. arr is an Array object.
Document. write (Array. isArray (str); // returns false. str is a string, not an Array object.

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.