To move an element in an divisor group

Source: Internet
Author: User

Problem analysis: The elements in the migration group can be divided into two cases,

1, do not operate the original array;

  

Do not manipulate the original array, you can create a new array, using the stack method of the array push, loop the elements in the array, the addition of a new array of elements that are not equal to a particular element.

function Remove (arr, item) {
var res=[];
for (Var i=0;i<arr.length;i++) {
if (Arr[i]==item) {continue}
else{
Res.push (Arr[i]);
}
}
return res;


}

2, the operation of the original array, you can take advantage of the operation of the array splice, note that the splice return value is the deleted item, at the same time, splice will change the length of the array, should be executed splice must perform I--。

function Removewithoutcopy (arr,item) {
for (Var i=0;i<arr.length;i++) {
if (Arr[i]==item) {
Arr.splice (i,1); i--;
}
}
return arr;
}

  

To move an element in an divisor group

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.