Array de-+indexof () application

Source: Internet
Author: User

Talking about the array to the weight of everyone is not strange, go to heavy also have a good variety of methods, here is a good understanding of the two.

First Kind

First of all, the first logic is to take the first to go with the second one, then the third one, and then the fourth one. As long as there is equal, you can use the splice () method to remove, this lap is more than finished, then take the second to go with the third ratio, and then the fourth ratio. So always compared to go down, until the judgment is complete, then a look is to two cycles, see the code.

var arr = [1,2,1,3,4,5,5];console.log (arr);   // 1,2,1,3,4,5,5  for (var i=0; i<arr.length; i++) {    for (var j=i+1; j<arr.length; j + +) {        if(arr[i] = = Arr[j]) {            Arr.splice (J,1);}}}    Console.log (arr);   // 1,2,3,4,5

By the way, the use of splice ():

(1) Delete function

Arr.splice (A, B): Starting from a (index) to intercept the B elements, directly in the original array is removed, the above to use the function of the heavy.

var box = [' Apple ', ' orange ', ' banana ']; var box2 = Box.splice (0,2//"Apple", "Orange"console.log (box);  // "Banana"

(2) Insert function

Arr.splice (A, B, c): The second parameter, B, is 0, then add the C element from a only without interception.

var box = [' Apple ', ' orange ', ' banana ']; var box2 = Box.splice (1,0, ' Mango '/// Insert an element at index 1 console.log (box);  // "Apple", "Mango", "orange", "banana"

(3) Replacement function

Arr.splice (A, B, c): B is not 0, the B element is intercepted from a (directly removed), and the intercepted element is replaced with a C.

var box = [' Apple ', ' orange ', ' banana ']; var = Box.splice (box2, ' Mango '//"Orange"console.log (box);  // "Apple", "Mango", "banana"

Do you have a deep understanding of this method?

The second Kind

The second is to use the IndexOf () method to achieve this effect, using its difference cannot find the item return-1 This feature, create an empty array, an item loop.

var arr = [1,2,1,3,4,5,5]; var s = [];  for (var i=0; i<arr.length; i++) {    if(S.indexof (arr[i]) = =-1) {        s.push (Arr[i] );    }} Console.log (s);   // 1,2,3,4,5

Speaking of IndexOf (), at this point I thought of an example, as a supplement, about finding where a character appears, the same principle.

var str = "You ying Yi Shang"; var strbox = []; var res = str.indexof (' y ');  while (Res >-1) {    Strbox.push (res);     = Str.indexof (' y ', res+1);} Console.log (Strbox);   // 0,4,9

Create an empty array strbox to hold the location of the searched character, define a variable first to get the position of the first searched Y, if it is greater than-1, add the index position to Strbox, and then continue searching after this position +1, if the value of res is still greater than-1, indicating that there is a Y presence, Continue to store, continue searching, until equal to-1, can output view.

Array de-+indexof () application

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.