2017/3/29_ Array Loop traversal problems

Source: Internet
Author: User

1. The following code implements the ability to pass in an ARR array (the elements in the ARR array may have the same), create a new arr_new array, iterate through the ARR array with a For loop, and then determine the elements that are not duplicated and put into the arr_new array.

1 //gets the contents of the array that are not duplicates by passing in the array2                 functionL_arr (Arr:array)3                 {4                     varArr_new:array =NewArray ();5                     varTemp:int = 0;6                      for(vari = 0; i < arr.length-1; i++ ){7                         if(arr[i][0]!=arr[i+1][0]){8                             9Arr_new[temp] = arr[i][0];Tentemp++; One                         } A                     } -Arr_new[temp] = arr[i][0]; -                     returnarr_new; the}

Attention:

If you accidentally write the 6th line in the above code for (var i = 0; i < arr.length; i++) , the following error will occur:typeerror:error #1010: The term has not been defined and no What property.

So in the future when writing code, you should pay attention to the bounds of the array traversal problem.

2. The function of the following code is to pass in the arr two-dimensional array, filtered to get all the remaining elements except arr[i][0].

1 //In addition to the first column, the rest of the content2                 functionF_arr (Arr:array)3                 {4                     varArr_new:array =NewArray ();5                     varArr_temp:array =NewArray ();6                      for(vari = 0; I < arr.length;i++ ){7                          for(varj = 1; J < Arr[0].length; J + + ){8ARR_TEMP[J-1] =Arr[i][j];9                         }TenArr_new[i] =arr_temp; OneArr_temp =NewArray (); A                     } -                     returnarr_new; -}

Note: If you define only a new array, the following is the case. There will also be an error:typeerror:error #1010: The term is not defined and has no attributes.

So in the future when the two-dimensional array to operate, pay attention to this problem.

1 //In addition to the first column, the rest of the content2                 functionF_arr (Arr:array)3                 {4                     varArr_new:array =NewArray ();5                     6                      for(vari = 0; I < arr.length;i++ ){7                          for(varj = 1; J < Arr[0].length; J + + ){8ARR_NEW[I][J-1] =Arr[i][j];9                         }Ten                      One                     } A                     returnarr_new; -}

2017/3/29_ Array Loop traversal problems

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.