1.Array creation
var arr=new Array();
2. Find elements in the array
for(var i=0;i<;i++)
if(arr[i]==temp)
return true;
3. The following is a comparison of the characteristics of Array and Object:
Array:
Create new:var ary = new Array(); or var ary = [];
Added:(value);
Delete: delete ary[n];
Traversal: for ( var i=0 ; i < ; ++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 Object can be used as a collection. In the menu for creating infinite Web pages using the Popup window (3), I have introduced the __MenuCache__ implemented by Eric, which is also a simulated collection object.
If we want to retrieve a specified value in Array, we need to iterate through the entire array:
var keyword = ;
for ( var i=0 ; i < ; ++i )
{
if ( ary[i] == keyword )
{
// todo
}
}
When we search for an entry for a specified key in Object, we just need to use:
var key = '';
var value = obj[key];
// todo
This feature of Object can be used to efficiently retrieve Unique string collections. The time complexity of traversing Array is O(n), and the time complexity of traversing Object is O(1). Although the cost of for searching for 10,000 sets is only a few dozen ms, if it is 1000*1,000 searches or more, the advantages of using Object will be reflected immediately. Before this, I made a mapping, mapping 100 Unique characters to 1000 string arrays, which took 25-30s! Later, the for traversal was changed to the member reference of the Object simulated collection. The same data mapping took only 1.7-2s!!!
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, try not to use for ( in ) traversal.
4.
shift: Delete the first item of the original array and return the value of the deleted element; if the array is empty, return undefined
var a = [1,2,3,4,5];
var b = (); //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 = (-2,-1); //a:[-2,-1,1,2,3,4,5] b:7
Note: The test return value in IE6.0 is always undefined, and the test return value in FF2.0 is 7, so the return value of this method is unreliable. When you need to use the return value, you can use splice instead of this method.
pop: Delete the last item of the original array and return the value of the deleted element; if the array is empty, return undefined
var a = [1,2,3,4,5];
var b = (); //a:[1,2,3,4] b:5
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 = (6,7); //a:[1,2,3,4,5,6,7] b:7
concat: Returns a new array, which is composed of adding parameters to the original array.
var a = [1,2,3,4,5];
var b = (6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
splice(start,deleteCount,val1,val2,...): DeleteCount item from the start position, and insert val1,val2,...
var a = [1,2,3,4,5];
var b = (2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
var b = (0,1); //Same shift
(0,0,-2,-1); var b = ; //Same unshift
var b = (-1,1); //Same pop
(,0,6,7); var b = ; //Same push
reverse: Inverse the array
var a = [1,2,3,4,5];
var b = (); //a:[5,4,3,2,1] b:[5,4,3,2,1]
sort(orderfunction): sort the array by specified parameters
var a = [1,2,3,4,5];
var b = (); //a:[1,2,3,4,5] b:[1,2,3,4,5]
slice(start,end): Returns a new array composed of items from the specified start subscript to the end subscript in the original array.
var a = [1,2,3,4,5];
var b = (2,5); //a:[1,2,3,4,5] b:[3,4,5]
join(separator): group the elements of the array into a string, with separator as the separator. If omitted, use commas as the separator by default.
var a = [1,2,3,4,5];
var b = ("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
Array is an internal object provided by JavaScript. It is a standard collection. We can add (push) and delete (shift) elements. We can also traverse the elements in it through a for loop.
var arr=new Array();
2. Find elements in the array
Copy the codeThe code is as follows:
for(var i=0;i<;i++)
if(arr[i]==temp)
return true;
3. The following is a comparison of the characteristics of Array and Object:
Array:
Create new:var ary = new Array(); or var ary = [];
Added:(value);
Delete: delete ary[n];
Traversal: for ( var i=0 ; i < ; ++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 Object can be used as a collection. In the menu for creating infinite Web pages using the Popup window (3), I have introduced the __MenuCache__ implemented by Eric, which is also a simulated collection object.
If we want to retrieve a specified value in Array, we need to iterate through the entire array:
Copy the codeThe code is as follows:
var keyword = ;
for ( var i=0 ; i < ; ++i )
{
if ( ary[i] == keyword )
{
// todo
}
}
When we search for an entry for a specified key in Object, we just need to use:
Copy the codeThe code is as follows:
var key = '';
var value = obj[key];
// todo
This feature of Object can be used to efficiently retrieve Unique string collections. The time complexity of traversing Array is O(n), and the time complexity of traversing Object is O(1). Although the cost of for searching for 10,000 sets is only a few dozen ms, if it is 1000*1,000 searches or more, the advantages of using Object will be reflected immediately. Before this, I made a mapping, mapping 100 Unique characters to 1000 string arrays, which took 25-30s! Later, the for traversal was changed to the member reference of the Object simulated collection. The same data mapping took only 1.7-2s!!!
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, try not to use for ( in ) traversal.
4.
shift: Delete the first item of the original array and return the value of the deleted element; if the array is empty, return undefined
var a = [1,2,3,4,5];
var b = (); //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 = (-2,-1); //a:[-2,-1,1,2,3,4,5] b:7
Note: The test return value in IE6.0 is always undefined, and the test return value in FF2.0 is 7, so the return value of this method is unreliable. When you need to use the return value, you can use splice instead of this method.
pop: Delete the last item of the original array and return the value of the deleted element; if the array is empty, return undefined
var a = [1,2,3,4,5];
var b = (); //a:[1,2,3,4] b:5
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 = (6,7); //a:[1,2,3,4,5,6,7] b:7
concat: Returns a new array, which is composed of adding parameters to the original array.
var a = [1,2,3,4,5];
var b = (6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
splice(start,deleteCount,val1,val2,...): DeleteCount item from the start position, and insert val1,val2,...
var a = [1,2,3,4,5];
var b = (2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
var b = (0,1); //Same shift
(0,0,-2,-1); var b = ; //Same unshift
var b = (-1,1); //Same pop
(,0,6,7); var b = ; //Same push
reverse: Inverse the array
var a = [1,2,3,4,5];
var b = (); //a:[5,4,3,2,1] b:[5,4,3,2,1]
sort(orderfunction): sort the array by specified parameters
var a = [1,2,3,4,5];
var b = (); //a:[1,2,3,4,5] b:[1,2,3,4,5]
slice(start,end): Returns a new array composed of items from the specified start subscript to the end subscript in the original array.
var a = [1,2,3,4,5];
var b = (2,5); //a:[1,2,3,4,5] b:[3,4,5]
join(separator): group the elements of the array into a string, with separator as the separator. If omitted, use commas as the separator by default.
var a = [1,2,3,4,5];
var b = ("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
Array is an internal object provided by JavaScript. It is a standard collection. We can add (push) and delete (shift) elements. We can also traverse the elements in it through a for loop.