Comparison of Two arrays in JS, clever method for deleting duplicate values (recommended), js Array

Source: Internet
Author: User

Comparison of Two arrays in JS, clever method for deleting duplicate values (recommended), js Array

Occasionally, we need to compare two arrays and Delete the existing values of the other array in one array. The method we often use is to compare and judge and delete cyclically. Recently we have seen another example of clever deletion of good methods:

Var arr1 = ["I", "B", "c", "d", "e", "f", "x"]; // array A var arr2 = ["a", "B", "c", "d", "e", "f", "g"]; // array B var temp = []; // temporary array 1 var temparray = []; // temporary array 2 for (var I = 0; I <arr2.length; I ++) {temp [arr2 [I] = true; // clever place: Treat the value of array B as the key of temporary array 1 and assign a value to true }; for (var I = 0; I <arr1.length; I ++) {if (! Temp [arr1 [I]) {temparray. push (arr1 [I]); // clever place: At the same time, the value of array A is treated as the key of the temporary array 1 and determines whether it is true. If it is not true, it indicates there are no duplicates, then, it is merged into a new array to obtain a completely new array with no duplicates} ;}; document. write (temparray. join (",") + "");

The clever method (recommended) for deleting duplicate values for comparing the above two arrays of JS is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

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.