JS Summary of various operations for array

Source: Internet
Author: User
Tags shallow copy
Array should be we in peacetime write JS code, the use of the highest frequency, in peacetime projects, a lot of data can be stored through an array, operation and other tasks. In addition to object, the array type should be the most commonly used type in JS.

Today, summarize some of the simple and basic operations of the array, and also consolidate your basic knowledge.

First, how to create an array (the following is directly said arrays)

There are two main ways to create an array, the first of which is to use an array constructor, and the second is to use the array literal notation.

1. Using Array constructors

such as: var arr = new Array ();

If you know the length of the array beforehand, you can also pass the length directly to the constructor.

For example: var arr = new Array (20);

If you know the items that should be included in the array, pass the items that should be contained in the array directly at the time of construction.

such as: var arr = new Array (All-in-all);

2, using the array literal notation

such as: var arr = [1,2,3,4];

var arr2 = [];

Ii. operation of arrays

1. Stack method and queue method

1) Stack operation mode: Advanced principle----Add data items by the end of the heavy array, and then get the trailing data items from the tail of the array

Push ();----is to add data items at the end of the array, the number of parameters of the method can be customized;

Pop ();---the method is to get the tail of the array of data items, the function does not need to pass any parameters;

Such as:

var colors = new Array ();//create array
var count = Colors.push ("Red", "green");//push in two
Console.log (count);

var color = ["Red", "Black"];
Color.push ("Brown");//Push in another
color[3]= "Yellow"; Add an item
Console.log (color);
Console.log (Color.push ("Blue"));
Console.log (Color.pop ());//Get the last one


2) How the queue operates: FIFO principle---Simulate implementation by inserting data from the head of an array and getting data items

Push ();--Add data items to the end of the array;

Shift ();---Gets the data information of an item in the head of an array;

Unshift ();--the opposite of shift is the insertion of data item information into the head of the array;

var Colorarr = new Array ();//create array
Colorarr.push ("Red", "yellow");//push in two
Console.log (Colorarr);
var length = Colorarr.push ("Blue");
Console.log (length);

var item = Colorarr.shift ();//Get first item
Console.log (item);
Console.log (colorarr.length);

2. Detecting and validating arrays

In peacetime project development, we often encounter, to determine whether an object is an array (function of the parameters passed), then if you determine whether an object is an array, there are the following two ways

1) The first method

if (value instanseof Array) {   }

2) The second method

if (Array.isarray (value)) {   }//This method only uses a browser with a higher version: ie9+, firefox4+/chrome

3, the specific programming example
1) add element (add element at end of array)

Add the element item at the end of the array arr. Do not directly modify the array arr, and the result returns a new array.

Method One: Slice () and push () combination

function Append (arr, item) {   var newArr = arr.slice (0);//Slice (start, end) shallow copy array   Newarr.push (item);   return NEWARR; };

Method Two: Normal iterative copy

function Append (arr, item) {   var length = arr.length,     newArr = [];     for (var i = 0; i < length; i++) {     newarr.push (arr[i]);   }     Newarr.push (item);     return NEWARR; };

Method Three: Use Concat

function Append (arr, item) {   return Arr.concat (item);}

2) add element (add an element of any position)
Adds an element item to the index of the array arr. Do not directly modify the array arr, and the result returns a new array.

Method One: Use normal iterative copies

function Insert (arr, item, index) {   var newarr=[];   for (Var i=0;i<arr.length;i++) {     newarr.push (arr[i]);   }   Newarr.splice (Index,0,item);   return NEWARR; }

Method Two: Slice () and splice () combination

function Insert (arr, item, index) {   var newarr=arr.slice (0);   Newarr.splice (Index,0,item);   return NEWARR; }

Method Three: Concat () and splice () combination

function Insert (arr, item, index) {   var newarr=arr.concat ();   Newarr.splice (Index,0,item);   return NEWARR; }

3. Delete the element (delete the last element of the array)
Deletes the last element of an array of arr. Do not directly modify the array arr, and the result returns a new array.

Method One: Use normal iterative copies

function truncate (arr, item) {   var newarr=[];   for (Var i=0;i<arr.length-1;i++) {     newarr.push (arr[i]);   }   return NEWARR; }

Method Two: Concat () and pop () combination

function truncate (arr) {   var newArr = Arr.concat ();   Newarr.pop ();   return NEWARR; }

4. Delete element (delete first element of array)
Deletes an array of arr first elements. Do not directly modify the array arr, and the result returns a new array.

Method One: Use normal iterative copies

function Curtail (arr) {   var newarr=[];   for (Var i=1;i<arr.length;i++) {     newarr.push (arr[i]);   }   return NEWARR; }

Method Two: Concat () and shift () combination

function Curtail (arr) {   var newArr = Arr.concat ();   Newarr.shift ();   return NEWARR; }

Method Three: Slice ()

function Curtail (arr) {   return Arr.slice (1);}

5. Merging arrays
Merges arrays arr1 and Arrays arr2. Do not directly modify the array arr, and the result returns a new array.

Method One: Use normal iterative copies

function concat (arr1, arr2) {   var newarr=[];   for (Var i=0;i<arr1.length;i++) {     newarr.push (arr1[i]);   }   for (Var j=0;j<arr2.length;j++) {     newarr.push (arr2[j]);   }   return NEWARR; }

Method Two: Concat () method

function concat (arr1, arr2) {   return arr1.concat (ARR2);}

Method Three: Slice () and push () combination

function concat (arr1, arr2) {   var newarr=arr1.slice (0);   for (Var i=0;i<arr2.length;i++) {     newarr.push (arr2[i]);   }   return NEWARR; }

5. Remove elements from the divisor group
Move an array of all the values in arr with an item equal to the element. Do not directly modify the array arr, and the result returns a new array.

Method One: Splice () method

function Remove (arr, item) {       var newArr = arr.slice (0);       for (var i=0; i<newarr.length; i++) {         if (newarr[i] = = Item) {           Newarr.splice (i, 1);         }       }       return NEWARR;     }     var arr = [1,2,3,4,2];     var item = 2;     Console.log (Remove (arr, item));     Console.log (arr);

Method two: Push () method

function Remove (arr,item) {   var newarr = [];   for (Var i=0;i<arr.length;i++) {     if (arr[i]! = Item) {       Newarr.push (arr[i]);}   }   return newarr; }
  • 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.