JS Array handler defragment

  • 2020-03-30 04:31:37
  • OfStack

Concat () concatenates two or more arrays
This method does not change the existing array, but simply returns a copy of the concatenated array.
Such as:


 <script type="text/javascript">
        var arr = [1, 2, 3];
        var arr1 = [11, 22, 33];
        document.write(arr.concat(4, 5, arr1));
 </script>

Output results:
1,2,3,4,5,11,22,33

2, the join ()
Put all the elements of the array into a string. The element is separated by the specified separator.
Such as:


 <script type="text/javascript">
       var arr = ['item 1', 'item 2', 'item 3'];
       var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>';
 </script>

List the results:

'< Ul> < Li> Item 1 < / li> < Li> Item 2 < / li> < Li> Item 3 < / li> < / ul> '
This is by far the fastest way! With native code (like join()), it's usually much faster than non-native code, regardless of what's going on inside the system. - James Padolsey, james.padolsey.com

3. Pop () removes and returns the last element of the array
The pop() method deletes the last element of the array, subtracts the array length by 1, and returns the value of the element it deleted.
If the array is already empty, pop() does not change the array and returns an undefined value
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.pop() + "<br/>");
       document.write(arr);
 </script>

Output results:
George, John Thomas
Thomas
George, John

4. Push () adds one or more elements to the end of the array and returns a new length
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.push("James") + "<br/>");
       document.write(arr);
 </script>

Output results:
George, John Thomas
4
George, John, Thomas, James

 

5. Unshift () adds one or more elements to the beginning of the array and returns a new length
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.unshift("James") + "<br/>");
       document.write(arr);
 </script>

Output results:
George, John Thomas
4
James, George, John Thomas

 

6. Reverse () reverses the order of the elements in the array
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.reverse());
 </script>

Output results:
George, John Thomas
Thomas, John, George

7. Shift () removes and returns the first element of the array
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.shift() + "<br/>");
       document.write(arr);
 </script>

Output results:
George, John Thomas
George
John, Thomas

Slice (start,end) returns the selected element from an existing array
Note that this method does not modify the array, but instead returns a subarray
Such as:


 <script type="text/javascript">
       var arr = ["George", "John", "Thomas"];
       document.write(arr + "<br/>");
       document.write(arr.slice(1) + "<br/>"); //Intercept from the first element to the end of the array
       document.write(arr);
 </script>

Output results:
George, John Thomas
John, Thomas
George, John Thomas

Sort () sorts the elements of an array
A reference to an array. Note that the array is sorted on the original array and no copy is made
This method is sorted by default in the order of character encoding (ASCII)
Such as:


 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = "John";
     arr[1] = "George";
     arr[2] = "Thomas";
     document.write(arr + "<br/>");
     document.write(arr.sort());
 </script>

Output results:
John, George, Thomas
George, John Thomas

Here's another example:


 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = 10
     arr[1] = 5
     arr[2] = 40
     arr[3] = 25
     arr[4] = 1000
     arr[5] = 1
     document.write(arr + "<br/>");
     document.write(arr.sort());
 </script>

Output results:
10,5,40,25,1000,1
1,10,1000,25,40,5

As you can see, instead of sorting by Numbers as we thought, if you want to sort by Numbers, you need to change the default sort and specify your own sort rules.
As follows:


 <script type="text/javascript">
     var arr = new Array(6);
     arr[0] = 10
     arr[1] = 5
     arr[2] = 40
     arr[3] = 25
     arr[4] = 1000
     arr[5] = 1
     document.write(arr + "<br/>");
     document.write(arr.sort(function (a, b) {return a - b;}));//From large to small
 </script>

Output results:
10,5,40,25,1000,1
1,5,10,25,40,1000
What if you want it in descending order?
Change the collation to:
Function (a, b) {return b-a; }
With respect to OK

10. Splice () removes the element and adds a new element to the array
The splice() method works differently from the slice() method, which directly modifies the array
(1) delete the array elements in the specified range:


 <script type="text/javascript">
     var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2, 3); //Three array elements (containing the third element) after the third element is removed
    document.write(arr);
 </script>

Output results:
George, John, Thomas, James, Adrew, Martin
George, John, Martin

(2) insert the specified element from the specified subscript (no limit on the number of elements) :


 <script type="text/javascript">
    var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2, 0, "William","JACK"); //Insert "William","JACK"
before the third element     document.write(arr);
 </script>

Output results:
George, John, Thomas, James, Adrew, Martin
George, John, William, JACK, Thomas, James, Adrew, Martin


(3) delete the array elements in the specified range and replace them with the specified elements (the number of elements is not limited) :


 <script type="text/javascript">
    var arr = new Array(6);
    arr[0] = "George";
    arr[1] = "John";
    arr[2] = "Thomas";
    arr[3] = "James";
    arr[4] = "Adrew";
    arr[5] = "Martin";
    document.write(arr + "<br/>");
    arr.splice(2,3,"William","JACK"); //Delete the three array elements after the third element (containing the third element) and replace
with "William" and "JACK"  document.write(arr);
 </script>

Output results:
George, John, Thomas, James, Adrew, Martin
George, John, William, JACK Martin


Related articles: