JavaScript common Operations Array functions

Source: Internet
Author: User
Tags constructor

Array manipulation functions

Join (delimiter) to use delimiters (delimiter) for each element in the array as a string
Concat (Array1, array2, ...) Merges the array, does not affect the original array, but returns the merged new array
Pop () deletes and returns the last element
Push (Element1, Element2, ...) Append element at end of array
Shift () deletes and returns the first element
Unshift (element1, Element2, ...) Add an element at the beginning of an array
Reverse () Reverses the order of array elements
Sort (sortby) sorting arrays
Slice (start, end) returns a new array, copying the elements between start, ending (excluding ends) to the new array
Splice (Start, Count, ReplaceElement1, ReplaceElement2, ...)

Deletes or replaces an array element, starting with start, and deleting or replacing count elements

If the replaceelement parameter is supplied, the replacement is done, otherwise the number of elements replaced and the number of replaceelements must not be equal
Returns an array that contains the elements that are deleted or replaced

Here I combine some functions to introduce the common examples of array operations


1. Delete the specified element in the array

The code is as follows Copy Code


/**
* Reference Examples
foreach = function (obj, Insp) {
if (obj== null && obj.constructor!= Array) {
return [];
}
obj is the array to process, Obj==null indicates that the object does not exist; Obj.constructor!= array represents the property of the object obj is not an array;
The constructor property always points to the constructor that creates the current object. Two conditions are satisfied, an empty array is returned [];

The constructor property is further understood below.
var obj= [1, 2, 3, 4]; Equivalent to Var obj= new Array (1, 2, 3, 4);
Console.log (Obj.constructor = = Array); Returns true to indicate that the constructor for obj is array;

var foo= function () {}; Equivalent to var foo = new Function ();
Console.log (Foo.constructor = = Function); Returns true to indicate that the constructor of Foo is a function;

var obj = new Foo (); Instantiating an Obj object by a constructor
Console.log (Obj.constructor = = = Foo); Returns true to indicate that the constructor for obj is foo;

*/

Deletes the specified element in the array

Function del (val, arr)
{
Detection parameters
if (arr = = null && arr.constructor!= Array) {
return [];
}

var newarr = []; Does not exist to save 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 duplicated elements in an array
the Key,key that saves the value of an element as a new array cannot be duplicated, and then the group of variables can be
 */
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 a) {
        data[data.length] = i;
  ;  }
    return data;
}
Alert (Unique ([12,12,12,34]));


3. Delete an array to specify an element for the subscript

The code is as follows Copy Code

/**
  * The value of the specified subscript element of the array
 *
 * i is changed, and the value of n is changed only when the IF condition is set (incremented 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,3,4,2,5];
A Lert (arr);
Arr.remove (2);

Alert (arr);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.