This article will give you a summary of the js array operation examples. These are basic js array operation methods. I hope this tutorial will be helpful to you.
1. Create an array
Var arr = []; // create an empty arrayVar arr1 = new Array (5); // create an Array and assign 5 lengths (null)Var arr2 = new Array ('php blog', 'javascript blog', 'mysql blog ','. net blog ', 'it blog'); // create an array with values
2. Read the value of the array
Var arr2 = new Array ('php blog ', 'javascript blog',
STD: List Learning
(The content of this article is from the Internet)List: splice () function details
List: splice implements list concatenation. Delete part or all of the elements in the source list and insert them to the destination list.
The function has the following three declarations:
Void splice (iterator position, list
Void
a negative value (-a) exists in the parameter, it indicates the position a relative to the last element in the array. For example, (-3) indicates the last and third elements to the end. If a negative number is displayed, it is first converted and then determined according to the range rules.It also returns a new array without modifying the original array.
1 2 3 4 5 6 7
Var arr = [1, 2, 4, 5]; Console. log (arr. slice (0, 3); // [1, 2, 3] Console. log (arr); // [1, 2, 3, 4
concat () method can be used to return the attributes of a new array for copying the array.var = [Num14]; var num15 = Num14.concat ([4,5],[6,7]); Alert ("when the parameter is multiple arrays:" +NUM15); // 1,2,3,4,5,6,7 var num16 = num14.concat (4,5); Alert ("when the parameter is non-array:" +NUM16); // 1,2,3,4,52.slice ()Slice () can create a new array based on one or more items in the current array, and return a new array.Slice () can receive one or two parameters, that is, the starting and
arr1 = [' A ', ' B ', ' C ']ARR2 = [' 1 ', ' 2 ', ' 3 ']Inserts the array arr2 into the array arr1 after the second element B;Ideas:Inserting a specific position, we first think of splice, but not directly splice (2, 0, arr2), the result: [' A ', ' B ', [' 1 ', ' 2 ', ' 3 '], ' C '];So how can we put arr2 elements of a splice into the arr1, there is no simple way
array directly.
or var arr=["Paramecium", "Love", "fluffy"];//parentheses can also declare an array object
Of course, like the C language, you can also define 2-D 3-dimensional arrays, which are not discussed here.
Properties of array: length
Arr.length returns the length of the array arr, which is common to the traversal of the arrays in the loop, such as:
for (Var i=0;iExecutive part}
Array element access: Arr[index], in which index represents the array cardinality, from 0, a total of a
, computer science and technology, and the added element.
Introduction to the article: array in JavaScript.
Basic operations on Arrays
1. concat () method
The concat () method can be used to create an array based on an existing number of groups to connect to the array.Var a1 = [1, 2, 3];
Var a2 = a1.concat (); // create a new array, add two elements to a1, and assign them to a2.
Alert (a1); // returns 1, 2, 3
Alert (a2); // returns 1, 2, 3, 4, 5
2. slice () method
The slice (
elements of the array fragment starting from the array,The second parameter specifies that the part ends with the elements of the array, but does not contain the elements at this position., 0 indicates the first element. If there is only one parameter, it indicates starting from the position specified by this parameter to all the remaining elements of the array. If the parameter has a negative number, it indicates the last element of the array. For example,-1 indicates the last element of the a
Use empty () instead of checking whether size () is empty
For any container C, write down
If (C. Size () = 0 )...
Essentially equivalent to writing
If (C. Empty ())...
This is an example. You may wonder why a constructor is better than another one, especially in fact, the typical Implementation of empty () is an inline function that returns size () whether to return 0.
You should first choose the empty () Construction, and the reason is very simple: For all the standard containers, empty () is
);//["Eee", "AAA", "BBB", "CCC"]
Intercept array Slice ()
Slice (intercept start position, intercept end position) Slice can have two parameters, the first parameter is required, the second parameter is optional, when there is only one parameter, the default intercept to the end of the array, and the second parameter can have positive and negative two cases, If the second argument is a negative number, the ending position of the intercept is counted from the end.1 vararr = [1,2,3,4
];Var B = a. shift (); // a: [2, 3, 4, 5] B: 1
Unshift: add the parameter to the beginning of the original array and return the length of the array.Var a = [1, 2, 3, 4, 5];Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7Note: In IE6.0, the test return value is always undefined, and in FF2.0, the test return value is 7. Therefore, the return value of this method is unreliable. You need to use splice instead of this method when returning the value.
Pop: D
This article mainly introduces array operations in JavaScript. This article describes join (), reverse (), sort (), concat (), slice (), and splice () for example, if you need several functions, you can refer to the array objects in JavaScript and use these methods to operate arrays.
Join ()
You can use the join () method to merge the members in the array into a string:
The code is as follows:
Var o = [1, 2, 3];Console. log (o. join (); // 1, 2
[Code sea shell JS] JS: delete elements in the array, sea shell jsPreface
To delete an element from a list in Java, you can simply use the remove Method.
The array in js does not have the remove method, but in js, the array has the splice method to achieve the same effect. In addition, you can also use other methods to achieve this effect.
Use the splice method to delete elements from the array
First, let'
, $ index){If ($ index This. unshift ($ value );Else if ($ index> = this. length)This. push ($ value );ElseThis. splice ($ index, 0, $ value );}/*** Delete an element based on the subscript of the array.*/Array. prototype. removeByIndex = function ($ n ){If ($ n Return this;} Else {Return this. slice (0, $ n). concat (this. slice ($ n + 1, this. length ));}}// Dependent on indexOfArray. prototype. remove = function ($ value){Var $ index = this. indexO
=7240b65810859cbf2a8d9f76a638c0a3partner=1900000109spbill_create_ip=196.168.1.1 total_fee=1sign=7F77B507B755B3262884291517E380F8",
The parameters required for package assembly are shown in the code above. Therefore, we need to prepare a params and a signature. The signature process is as follows:
1. Sort params in the Lexicographic Order of keys and splice them into strings. Note that these keys do not include the sign
2. After the above string,
Clause 4: Use empty () instead of checking whether size () is 0
For any container C, write down
if (c.size() == 0)...
Essentially equivalent to writing
if (c.empty())...
This is an example. You may wonder why a constructor is better than another one, especially in fact, the typical Implementation of empty () is an inline function that returns size () whether to return 0.
You should first choose the empty () Construction, and the reason is very simple: For all the standard containers, empty () is
-dimensional, 3-dimensional, and multi-dimensional arrays, which are not discussed here.
Attribute of the array: LengthArr. Length returns the length of the array arr, which is common in traversing arrays in a loop. For example:For (VAR I = 0; I Execution part}Access to array elements: arr [Index], where index indicates the base number of the array. It starts from 0 and has a total of arr. length element. for example, if arr [0] accesses the first array element, arr [1] accesses the second array
array. If a negative number appears in the argument, it represents the position of the last element in relation to the array only. For example, parameter 1 specifies the last element, and 3 specifies the third-to-bottom element. Note that slice () does not modify the called array. Here are some examples:var a = [1,2,3,4,5];A.slice (0,3); back to [+]A.slice (3); Back to [4,5]A.slice (1,-1); Back to [2,3,4]A.slice ( -3,-2); Back [3]6.splice ()The Array
"]Grammararray. Splice ( start ) array . Splice ( start deleteCount array . Splice ( start deleteCount item1 item2 ,...)Parameters
start?
Specifies the start position of the modification (counted from 0). If the length of the array is exceeded, the content is added from the end of the array, or, in the case of a negative value, the nu
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.