JavaScript obtains a simple example of the intersection of multiple arrays_javascript skills
WBOY
Release: 2016-05-16 17:16:19
Original
1300 people have browsed it
In the project, it is necessary to obtain the intersection of multiple arrays, so this example is only suitable for specific scenarios. For example, A array var a = {1000,10001,10002,10003}; B array var b = {10002, 10003}; C array var c = {10003}; You need to obtain the intersection array of these three arrays. The specific idea is: first initialize the smallest array as the A array, and the smallest number of elements is also the length of the array A. Then iterate through each array, obtain the shortest length of these arrays as the minimum length, and at the same time obtain the shortest length array . Then, iterate the minimum array and each array, start to compare the elements for equality, and use a counter to determine whether the element exists in each array and it is an intersection element. The idea is relatively simple, but it can be achieved to obtain intersection with multiple arrays. The code is as follows:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn