3 Move the divisor group all elements in arr that are equal to item, and do not change the original array.

Source: Internet
Author: User

Method One: Filter ES5 method

var a=[1,5, ' ff ', ' g ', ' h ', ' SD '];alert (remove (A, ' G '));         function  Remove (arr,item) {    return   arr.filter (function (ele) {return ele! =Item})}  

Method Two: Push method

function  Remove (arr,item) {    var arr2=[];    for (var i=0;i<arr.length; i++)    {        if (arr[i]!= Item)            {            arr2.push (arr[i]);        }    }    return arr2;}    

Method Three: Splice method

// The trick of this method is to delete the equal element item backwards, avoiding the problem of moving the element forward when it is deleted; function   Remove (arr,item) {  var newarr=arr.slice (0);    for (var i=newarr.length;i>=0; i-- )  {            if (newarr[i]==Item)      {                  Newarr.splice (i,1);                         }  }   return   newarr;}

Method Four: Using the characteristics of the array itself, the array as a queue, using the push and shift methods to operate;

function Remove (Arr,item) {
var newarr=arr.slice (0);
for (var i=0;i<newarr.length+1;i++) //Note i<newarr.length+1; here
{

if (Newarr[0]!==item)
{
Newarr.push (Newarr[0]);
Newarr.shift ();
}
else{
Newarr.shift ();
}
}
return newarr;
}

3 Move the divisor group all elements in arr that are equal to item, and do not change the original array.

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.