Js tutorial-javascript array

Source: Internet
Author: User
Tags javascript array
An array is a collection of data. in the memory, it is a continuous memory address. js arrays are divided into one-dimensional arrays and multi-dimensional arrays.

JAVASCRIPT array

1. what is an array?

An array is a collection of data. in the memory, it is a continuous memory address. js arrays are divided into one-dimensional arrays and multi-dimensional arrays.

1. create an array

  • Var arr = [value 1, value 2, value 3];
  • Var arr = new Array (value 1, value 2, value 3 );
  • Var array = new Array (size );



/Defines an array named arr1 // contains three array elements // their values are zhangsan, lisi, and wangwuvar arr1 = ['zhangsan', 'lisi ', 'wangwu']; // defines an Array named arr2 // contains two Array elements // their values are xiaoqiang and wangcaivar arr2 = new Array ('xiaoqiang ', 'hangcai '); // defines an Array named arr3 // contains three Array elements // their current values are empty var arr3 = new Array (3 ); // further interpret the array defined in js var str = 'hello'; // it is actually a class object of the String class, therefore, you can also write var str1 = new String ('Hello'); var a1 = [1, 2, 4]; // it is actually a class object of the Array class, therefore, you can also write var a2 = new Array (,); var i1 = 10; // Actually, I is a class object of the Number class, therefore, you can also write var i2 = new Number (10 );

Typeof: test data type 2. test data type



var age=10;var name='xiaoqiang';var marry=true;var arr=[10,'xiaoqiang',true];document.write(typeof age);document.write('
');document.write(typeof name);document.write('
');document.write(typeof marry);document.write('
');document.write(typeof arr);
Array name. attribute in JS, the Array variable belongs to the object data type (object data type) and belongs to the class object of the Array class, so the Array can be used as follows:


Array name. method ();

1. array length

  • Length: indicates the length of the array (number of array elements)

In Js, the length of the array can be dynamically set.

For example, increasing the length of an array also increases the number of elements in the array.

For example, if the length of an array is reduced, the corresponding elements are removed from the end of the array.

3. traverse the array

In js, there are two methods to traverse the array:

1) for loop

2)... In statement js array traversal dedicated

  • For (var I in array ){

}

Note: If a text subscript exists in an array, the corresponding text subscript element cannot be output when the for loop value is used. try to use the for... in statement to traverse this array.

IV. text subscript

  • Format: arr ['key']
  • The array element of the text subject is not included in the array length.
  • You need to use the for... in statement for traversal.
  • You can use the object name to call a text subscript to call an element.

# P # php Tutorial-javascript programming-array functions # e #

5. array functions

1. add array elements

  • ArrayObject. push (newelement1, newelement2,..., newelementX) adds one or more array elements to the end of the array.
  • ArrayObject. unshift (newelement1, newelement2,..., newelementX) add one or more array elements at the beginning of the array
  • ArrayObject. splice (index, howmany, element1,..., elementX) adds one or more array elements to an array position.

Index: location

Howmary: number of replicas (set to 0)

2. delete array elements

  • ArrayObject. pop () deletes the last array element
  • ArrayObject. shift () deletes the first array element
  • ArrayObject. splice (index, howentries) deletes the elements at the specified position.

Index: the start position of the deletion.

Howmary: how many


// Define an array var arr = ['hangsan', 'lisi', 'hangzhou']; // add wangcai to the final arr of the array. push ('wangcai '); // Print the element value display (arr); // add xiaoqiang to the beginning of the array, and the array automatically sorts the subscript arr. unshift ('xiaoqiang '); // Print the display (arr); // Insert the two elements from the first position to the array arr. splice (2, 0, 'huangliu', 'huqi '); display (arr );
ArrayObject. slice (start [, end]) intercepts part of the array. 3. truncates or merges arrays.


Start: Start position

End: End position (-1)

  • ArrayObject. concat (arrayX, arrayX,..., arrayX) merge arrays

Adding and deleting arrays means directly updating the array variables.

An array is returned when an array is truncated or merged.


Var arr = ['hangsan', 'lisi', 'hangzhou', 'xiaoqiang ', 'hangcai']; // 2: Start position // 4: end position, -1 is required, so the location here is 3arr1 = arr. slice (2, 4); display (arr1); // combine the elements in the arr array and the arr2 array var arr2 = ['huangliu', 'huqi']; // add the merged array to arr3. var arr3 = arr. concat (arr2); display (arr3 );
ArrayObject. sort () sorting. by default, it is sorted in ascending order. 3. array elements are sorted.


  • ArrayObject. reverse () invert array elements

4. functions related to array and string conversion

  • ArrayObject. join (separator) connects array elements using the specified separator, and returns the string
  • StringObject. split (separator, howator) splits the string according to the specified separator, and returns the string


// Define an array var arr = ['hangsan', 'lisi', 'hangzhou', 'xiaoqiang ', 'hangcai']; // use the-_-symbol as the separator to connect the array elements and return a string var str = arr. join ('-_-'); // Print the string document. write (str +'
'); // Define a string. use | to separate var str = 'zhangsan | lisi | wangwu'; // use the | separator to cut the string, returns a new array var arr1 = str. split ('|'); // Print the array display (arr1 );

Tip: 6. job: use js to complete level 2 Association in a region.


Option Class: (Option Class)

  • L var object = new Option (text [, value]);
  • Object Name. options

Options: Set of option objects

  • Object Name. options. add (option object) add option

Beijing

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.