JS array basic knowledge function declaration element operation

Source: Internet
Author: User

The constructor of an array

Because of the uncertainty of the array's constructors in handling parameters, it is strongly recommended that you use the [] symbol to create a new array.

The code is as follows Copy Code

[1, 2, 3]; Result: [1, 2, 3]
New Array (1, 2, 3); Result: [1, 2, 3]

[3]; Result: [3]
New Array (3); Result: []
New Array (' 3 ')//Result: [' 3 ']

When only one parameter is passed to the constructor of the array, and this argument is still a number, the constructor returns an array with the element value undefined, and the length property of the array is set to the numeric parameter of the Passed-in constructor. But actually the index of the new array is not initialized.

This usage can be used only in rare cases, such as looping strings, to avoid using a loop.

The code is as follows Copy Code

New Array (count + 1). Join (Stringtorepeat);

Summarize

To sum up, we should try to use [] to create a new function, rather than an array constructor, so that the code will be more readable.

Data Common operations

Since the original text of this blog is relatively short, I intend to summarize some common methods of array operation:

adding elements

1.push-Adds one or more new elements to the end of the array and returns the new length of the array.

2.unshift-Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned.

3.splice-Inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to [].

Delete Element

1.pop-Removes the last element and returns the element value.

2.shift-Removes the first element and returns the element value, and the elements in the array are moved forward automatically.

3.splice-Deletes the specified number of DeleteCount elements starting at the specified position deletepos, and returns the removed element as an array. (Note the difference between parameters when adding elements)

Traversing an array

The code is as follows Copy Code

var dd = {
' A ': ' 111111 ',
' B ': ' 222222 ',
' C ': ' 333333 '
}
dd[' d '] = [' 444444 ', ' 55555555 '];
Traversing an array
function Scan_array (arr) {
for (var key in arr) {//This is the key
if (typeof (Arr[key]) = = ' array ' | | typeof (Arr[key]) = = ' object ') {//Recursive call
Scan_array (Arr[key]);
} else {
document.write (key + ' = ' + Arr[key] + ' <br> ');
}
}
}

Other actions

1.join-Returns a string that joins each element value of the array, separated by the separator argument in the middle.

2.slice-method to return a fragment or a child array in an array, if only one argument is returned to the end of the array, if the argument is negative, the tail counts from the array, and if start is greater than end returns an empty array, slice does not change the original array, but instead returns a new array.

3.concat-Joins multiple arrays (which can also be strings, or a mixture of arrays and strings) into an array, returning a new array that is well connected.

4.reverse-Reverses the element (the top row to the last, last row to the top), and returns the modified array.

5.sort-Sorts the array elements, returning the modified arrays. When there are no parameters, the alphabetical order is sorted alphabetically, or a sort method can be passed in.

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.