1. shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. shift (); // a: [2, 3, 4, 5] B: 1
2. 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: 7
Note: 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.
3. pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. pop (); // a: [1, 2, 3, 4] B: 5
4. push: add the parameter to the end of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. push (6, 7); // a: [1, 2, 3, 4, 5, 6, 7] B: 7
5. concat: returns a new array consisting of adding parameters to the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. concat (6, 7); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5, 7]
6. splice (start, deleteCount, val1, val2,...): Delete the deleteCount item from the start position, and insert val1, val2 ,...
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // a: [,] B: []
Var B = a. splice (0, 1); // same as shift
A. splice (0, 0,-2,-1); var B = a. length; // same as unshift
Var B = a. splice (a. length-1, 1); // same as pop
A. splice (a. length, 7); var B = a. length; // same as push
7. reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]
8. sort (orderfunction): sorts arrays by specified parameters.
Var a = [1, 2, 3, 4, 5];
Var B = a. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]
9. slice (start, end): returns a new array consisting of items from the specified start subscript to the end subscript in the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]
10. join (separator): groups the elements of the array into a string that uses the separator "separator". If it is omitted, it is separated by commas by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"
An array is an internal object provided by JavaScript. It is a standard set. We can add (push), delete (shift) elements, we can also traverse the elements in the for loop, so can we have other sets in addition to arrays in JavaScript?
Because of the language features of JavaScript, We can dynamically add and delete attributes to common objects. Therefore, objects can also be seen as a special set of JS. The following compares the features of Array and Object:
Array:
New: var ary = new Array (); or var ary = [];
Added: ary. push (value );
Delete: delete ary [n];
Traversal: for (var I = 0; I <ary. length; ++ I) ary [I];
Object:
New: var obj = new Object (); or var obj = {};
Added: obj [key] = value; (key is string)
Delete: delete obj [key];
Traversal: for (var key in obj) obj [key];
From the above comparison, we can see that the Object can be used as a set. in the use of the Popup window to create an infinitely webpage menu (3), I introduced the _ MenuCache __implemented by Eric __, it is also a simulated set object.
If we want to retrieve a specified value in Array, We need to traverse the entire Array:
Code:
Copy codeThe Code is as follows: var keyword =;
For (var I = 0; I <ary. length; ++ I)
{
If (ary [I] = keyword)
{
// Todo
}
}
However, to retrieve a specified key entry in an Object, you only need to use:
Code:Copy codeThe Code is as follows: var key = '';
Var value = obj [key];
// Todo
This feature of the Object can be used to efficiently retrieve the string set of Unique. The time complexity of traversing the Array is O (n), and the time complexity of traversing the Object is O (1 ). Although the for search cost for 10000 sets is dozens of ms, if it is 1000*1000 queries or more, the advantages of using objects are shown. Before that, I made a mapping to map 100 Unique characters to 1000 string arrays, which took 25-30 s! Later, I changed the for traversal to the member reference of the Object simulation set. The same data volume mapping takes only 1.7-2 s !!!
For the collection traversal efficiency (from high to low): var value = obj [key];> for (;)> for (in ). The worst efficiency is for (in). If the set is too large, do not use for (in) traversal.