简体   繁体   中英

Find similarities and differences in a variable set of javascript array of arrays.

I'm trying to make a sort of Venn Diagram and I have an array of arrays that stores the data that is in each unique set. So something like:

array[0] = [1, 2, 3, 4]
array[1] = [2, 3, 4, 5]
array[2] = [3, 4, 6, 10, 15, 20]
array[3] = [15, 20, 25]

There could be up to 10 arrays within this original array set.

What I'm trying to do is to find similarities between sets but also have the ability to get the exclude sets. For example, if I want to find the similarities between the first and second array in the example above but exclude the third array, I would end up getting: [2] but not [3, 4] since it is not in the third set. I would want all possibilities to be available: selecting as many arrays and excluding as many arrays.

Is there an easy way of doing this? I was thinking in the lines of jquery with classes. Attaching each entry of the array and excluding everything was asked to be excluded. But I don't know if that's the correct way of going about it. Would I have to read each content of each array and combine them?

Look at the underscore.js library which allows you to do set manipulation like:

_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]

and

_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
=> [1, 2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM