This article will introduce you to the array functions commonly used in JavaScript. The most common functions are join, concat, pash, shift, sort, and so on, I will introduce it to you below.
Array Operation Functions
Join (delimiter) concatenates each element in the array into a string using delimiter.
Concat (array1, array2 ,...) The merged array does not affect the original array, but returns the New merged array.
Pop () deletes and returns the last element.
Push (element1, element2 ,...) Append an element to the end of an array
Shift () Delete and return the first element
Unshift (element1, element2 ,...) Add elements at the beginning of the array
Reverse () reverse the first and last order of array elements
Sort (sortby) sorts Arrays
Slice (start, end) returns a new array, and copies the elements between start and end (excluding end) to the new array.
Splice (start, count, replaceElement1, replaceElement2 ,...)
Delete or replace array elements. From start, delete or replace count elements.
If the replaceElement parameter is provided, replace it. Otherwise, delete the replaceElement. The number of replaced elements and replaceElements do not have to be equal.
Returns an array containing the deleted or replaced elements.
Next I will introduce some common array operation examples in combination with some functions.
1. Delete the specified element from the array.
The Code is as follows: |
Copy code |
/** * Reference instance Foreach = function (obj, insp ){ If (obj = null & obj. constructor! = Array ){ Return []; } // Obj is the array to be processed. obj = null indicates that the object does not exist. obj. constructor! = Array indicates that the constructor of the object obj attribute is not an Array; // The constructor attribute always points to the constructor that creates the current object. If both conditions are met, an empty array [] is returned.
// Learn more about the constructor attributes. Var obj = [1, 2, 3, 4]; // equivalent to var obj = new Array (1, 2, 3, 4 ); Console. log (obj. constructor === Array); // returns true, indicating that the obj constructor is Array; Var foo = function () {}; // equivalent to var foo = new Function (); Console. log (foo. constructor === Function); // returns true, indicating that the foo constructor is a Function; Var obj = new Foo (); // an obj object is instantiated by the constructor. Console. log (obj. constructor = Foo); // return true, indicating that the obj constructor is Foo; */ // Delete the specified Element in the array Function del (val, arr) { // Check parameters If (arr = null & arr. constructor! = Array ){ Return []; }
Var newarr = []; // Save the nonexistent values to the new array. For (var I = 0; I <arr. length; I ++ ){ If (arr [I]! = Val) Newarr. push (arr [I]); } Return newarr; } Alert (del (2, [1, 2, 3, 4, 5, 2]); |
2. Remove duplicate elements
The Code is as follows: |
Copy code |
/** * Remove repeated elements from the array Save the element value as the key of a new array, and the key cannot be repeated. Then, set the number of variables. */ Function unique (data ){ Data = data | []; Var a = {}; Len = data. length; For (var I = 0; I <len; I ++ ){ Var v = data [I]; If (typeof (a [v]) = 'undefined '){ A [v] = 1; } }; Data. length = 0; For (var I in ){ Data [data. length] = I; } Return data; } Alert (unique ([12, 12, 12, 34]); |
3. Delete the element of the specified base object in the array.
The Code is as follows: |
Copy code |
/** * Delete the specified subscript element of the array. * * The value of I is always changing, and the value of n is changed only when the if condition is true (it will increase sequentially) */ Array. prototype. remove = function (dx) { If (isNaN (dx) | dx> this. length) {return false ;} For (var I = 0, n = 0; I <this. length; I ++) { If (this [I]! = This [dx]) { This [n ++] = this [I] } } This. length-= 1 } Var arr = [1, 2, 4, 2, 5]; Alert (arr ); Arr. remove (2 ); Alert (arr ); |