我将尝试解释我需要做什么,为什么解释起来不那么简单。
我有4个电子邮件列表,其中有重复的项目,需要提取这些电子邮件,例如:
list 1:
email1, email2, email4, email5
list 2:
email2, email3, email5, email6
list 3:
email1, email2, email7, email8
list 4:
email2, email4, email6, email7
我需要这个: 结果:
email1 (list1, list3)
email2 (list1, list2, list3, list4)
email3 (list2)
...
我正在尝试使用javascript,因为实际上这些电子邮件都是excel ..
JSbin: http://jsbin.com/zerexuviya/edit?html,js,console,output
答案 0 :(得分:1)
Here is the solution I've just wrote using only Underscore.js: https://jsfiddle.net/alienpavlov/b0zaz5Lw/
var all = {
"list1": [
"email1",
"email2",
"email3",
"email4",
"email5",
"email6"
],
"list2": [
"email2",
"email3",
"email6",
"email7"
],
"list3": [
"email1",
"email3",
"email4",
"email5",
"email6"
],
"list4": [
"email1",
"email3",
"email5",
"email6"
]
};
var listOfAllEmails = _.union.apply(undefined, _.values(all));
_.each(listOfAllEmails, function(email, i) {
var result = [];
_.each(all, function(list, j) {
if (_.indexOf(list, email) > -1) {
result.push(j);
}
});
if (result.length > 1) {
//console.log(email, "=>", result);
var div = document.getElementById("result")
div.innerHTML = div.innerHTML + email + " (" + result + ")<br>";
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="result"></div>
It was all about sorting arrays. If you have any questions feel free to ask in comments.