我正在开发一个程序,并希望对 Javascript 的区别有一点了解。在我的程序中,我有一个黑名单数组,其中包含不允许出现在最终结果中的值,如下所示:
blacklist = ["One", "Two", "Four"]
someArr = ["One", "Three", "Five", "Two", "One"]
//Desired result = ["Three", "Five"]
我从另一个 Stack 问题中找到了一个很棒的提示(代码如下所示)。
Array.prototype.diff = function(a) {
return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};
////////////////////
// Examples
////////////////////
[1,2,3,4,5,6].diff( [3,4,5] );
// => [1, 2, 6]
["test1","test2","test3","test4","test5","test6"].diff(["test1","test2","test3","test4"]);
// => ["test5", "test6"]
问题是我需要将字符串数组与 3 维数组中的特定键进行比较。这是我的结构:
Array1 = [collab1: "Name, Someone's",
collab2: "Else, Somebody",
...: ...],
[collab1: "Else, Somebody",
collab2: "Thornton, Billy Bob",
...: ...];
Array2 = ["Name, Someone's", "Else, Somebody", "Thornton, Billy Bob"]
我想使用 diff 类对照 Array2 检查每个索引的 collab1。这可能吗?