An example of the use of Array in JS

  • 2020-03-30 02:05:31
  • OfStack

New Array ()
New Array (len)
New Array ([item0, [item1, [item2,...]]]
Methods to use array objects:
Var objArray = new Array ();
ObjArray. Concact ([item1 [, item2 [,...]]] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - connect the argument list to the back of the objArray form and returns a new array, the original array are not affected. Such as: var arr = [" a ", "b", "c");
Arr. Concact (" d ", "e");
Returns an array of elements containing the letters "a" through "e". Arr itself is unaffected.
Objarray.join (separator)- converts an array to a string, using the character specified in separator as the separator separator.
Objarray.pop ()- in layman's terms, is the last element of a pop-up array. Combined with the following push method, it is possible to use an array as a stack. The pop method returns the value of the last element of the array and subtracts the length attribute by 1, which immediately loses the last element.
ObjArray. Push ([value1, value2 [[,...]]]] -- -- -- -- -- -- -- -- -- -- -- -- -- add parameters to the end of the array. For example: [1,2,3,4]. Push ("a","b") will get [1,2,3,4,"a","b"].
Objarray.reverse () reverses the arrangement of the elements in an array. For example: [1,2,3]. Reverse () will get [3,2,1], which is a row operation on the original array and returns the array itself
Objarray.shift ()- removes the first element of the array and returns the value of that element. The properties of this method are very similar to the pop method, which removes the last element.
Objarray.slice (start,end)- returns a subset of the array object. The index starts at start and ends at end. Slice (1,2,3,4,5,6). Slice (1,4) will get [2,3,4]. When start or end is negative, they are used plus length. For example: [1,2,3,4,5,6]. Slice (-4,-1) will get [3,4,5]. If end is less than or equal to start, an empty array is returned.
Objarray.sort (comparefn)- sorts an array according to the size comparison function defined by comparefn. The function comparefn must take two arguments element1,element2, and return a negative number if you want element1 to precede element2. If you want element1 to come after element2, you should return a positive number, or 0 if the two Numbers are treated equally (that is, in the same order). When comparefn is omitted, the elements are arranged in dictionary order. Function CMP (e1,e2){return e1-e2; } [3,4,2,7]. Sort (CMP) will get [2,3,4,7].
Objarray.splice (start,deleteCount[,item1,item2[,...]]). Where, the start parameter represents the index position to be operated on, deleteCount refers to the number of elements to be deleted from start (including the start position), and if deleteCount is omitted, it means the remaining part of the array to be deleted from start. [,item1[,item2[,...]]] represents the optional list of elements inserted before start. Such as:
Var arr =,1,2,3,4,5,6 [0];
Arr. Splice (1, 1);
Document. The write (arr); // shows "0,2,3,4,5,6"
Arr =,1,2,3,4,5,6 [0];
Arr. Splice (0, 0, "a", "b");
Document. The write (arr); / / display "a, b, 0,1,2,3,4,5,6"
Arr =,1,2,3,4,5,6 [0];
Arr. Splice (3, 2, "c", "d");
Document. The write (arr); / / show "0, c, d, 5 or 6"
ObjArray. Unshift (item1, item2 [[,...]]]) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- will be inserted into the array to the beginning of the parameter list. Its nature and the type of push method, but a push method adds an element to the end of the array. Such as: [1, 2, 3, 4] unshift (" a ", "b") will be [" a ", "b", 1, 2, 3, 4].

Js array elements add and delete has been more confused, today finally found the detailed information, first give me a test code ^-^
Var arr = new Array();
Arr [0] = "aaa".
Arr [1] = "BBB";
Arr [2] = "CCC";
/ / alert (arr. Length); / / 3
Arr. Pop ();
/ / alert (arr. Length); / / 2
/ / alert (arr/arr. Length - 1); / / BBB
Arr. Pop ();
/ / alert (arr/arr. Length - 1); / / aaa
/ / alert (arr. Length); / / 1

Var arr2 = new Array();
/ / alert (arr2. Length); / / 0
Arr2 [0] = "aaa".
Arr2 [1] = "BBB";
/ / alert (arr2. Length); / / 2
Arr2. Pop ();
/ / alert (arr2. Length); / / 1
Arr2 = arr2. Slice (0, arr2. Length - 1);
/ / alert (arr2. Length); / / 0
Arr2 [0] = "aaa".
Arr2 [1] = "BBB";
Arr2 [2] = "CCC";
Arr2 = arr2. Slice (0, 1);
Alert (arr2. Length); / / 1
Alert (arr2 [0]); / / aaa
Alert (arr2 [1]); / / undefined

Shift: deletes the first item of the original array and returns the value of the deleted element. Returns undefined if the array is empty
Var a = [1, 2, 3, 4, 5].
Var b = a.s hift (); //a: [2,3,4,5] b: 1

Unshift: adds an argument to the beginning of the original array and returns the length of the array
Var a = [1, 2, 3, 4, 5].
Var b = a.u nshift (2, 1); //a: [-2,-1, 1,1,2,3,4,5] b: 7
Note: the test return value under IE6.0 is always undefined, and the test return value under FF2.0 is 7. Therefore, the return value of this method is not reliable. Splice can be used instead of this method when the return value is used.

Pop: deletes the last item in the original array and returns the value of the deleted element. Returns undefined if the array is empty
Var a = [1, 2, 3, 4, 5].
Var b = Amy polumbo op (); //a: [1,2,3,4] b: 5// call it directly if you don't need to return

Push: adds an argument to the end of the original array and returns the length of the array
Var a = [1, 2, 3, 4, 5].
Var b = Amy polumbo ush (6, 7); //a: [1,2,3,4,5,6,7] b: 7

Concat: returns a new array that adds arguments to the original array
Var a = [1, 2, 3, 4, 5].
Var b = a.c oncat (6, 7); //a: [1,2,3,4,5] b: [1,2,3,4,5,6,7]

Splice (start, deleteCount, val1, val2,...). : delete the deleteCount item from the start position and insert val1,val2...

When you empty the array, you simply pass startIndex.

If you do not delete all elements, then pass the deleteCount parameter.

Splice also has the function of first deleting and then adding, that is, first deleting a few elements, and then adding a number of elements in the deleted location, delete and add the number of elements must not be equal, when hou deleteCount is also used.
Var a = [1, 2, 3, 4, 5].
Var b = a.s plice,2,7,8,9 (2); //a: [1,2,7,8,9,5] b: [3,4]
Var b = a.s plice (0, 1); / / the same shift
A.s plice (0, 0, 2, 1); Var b = a. ength; / / with the unshift
Var b = a.s plice (a. ength - 1, 1); / / with pop
A.s plice (a. ength, 0, 6); Var b = a. ength; / / with a push

Reverse: reverse order the array
Var a = [1, 2, 3, 4, 5].
Var b = a.r everse (); //a: [5,4,3,2,1] b: [5,4,3,2,1]

Sort (orderfunction) : sorts the array with the specified parameters
Var a = [1, 2, 3, 4, 5].
Var b = a.s ort (); //a: [1,2,3,4,5] b: [1,2,3,4,5]

Slice (start,end) : returns a new array of items from the original array that specify the start index to the end index
Var a = [1, 2, 3, 4, 5].
Var b = a.s lice (2, 5); //a: [1,2,3,4,5] b: [3,4,5]

Join (separator) : groups the elements of an array into a string. Separator is the separator character
Var a = [1, 2, 3, 4, 5].
Var b = a. oin (" | "); //a: [1,2,3,4,5] b: "1|,2 |,3 |,4 |,5"

An array to simulate the javaStringBuffer string processing method:
 
 
function StringBuffer() { 
var arr = new Array; 
this.append = function(str) { 
arr[arr.length] = str; 
}; 

this.toString = function() { 
return arr.join("");//Ping the array append comes in as a string
}; 
} 

Today, I suddenly found that join is a good way to convert an array into a string in my application, so I used:
 
 
function arrayToString(arr,separator) { 
if(!separator) separator = "";//Separator for null defaults to empty
return arr.join(separator); 
} 

 
function arrayFindString(arr,string) { 
var str = arr.join(""); 
return str.indexOf(string); 
} 

Related articles: