阵列交集(set-theory)与Array.prototype.reduce

时间:2016-06-02 13:42:53

标签: javascript arrays ecmascript-6

我有两个数组Home --------------------------------------- <a tabindex="-1" title="Home" href="/share/page/site/kennis-databank/wiki-page?title=Main_Page" class="alfresco-navigation-_HtmlAnchorMixin">Home</a> --------------------------------------- --------------------------------------- My Files --------------------------------------- <a tabindex="-1" title="My Files" href="/share/page/context/mine/myfiles" class="alfresco-navigation-_HtmlAnchorMixin">My Files</a> --------------------------------------- --------------------------------------- Shared Files --------------------------------------- <a tabindex="-1" title="Shared Files" href="/share/page/context/shared/sharedfiles" class="alfresco-navigation-_HtmlAnchorMixin">Shared Files</a> --------------------------------------- --------------------------------------- Sites --------------------------------------- <span id="HEADER_SITES_MENU_text" data-dojo-attach-point="containerNode,textDirNode">Sites</span> --------------------------------------- --------------------------------------- ? <= sub menu items, only ? but no content --------------------------------------- ? <= sub menu items, only ? but no content --------------------------------------- --------------------------------------- Tasks --------------------------------------- <span id="HEADER_TASKS_text" data-dojo-attach-point="containerNode,textDirNode">Tasks</span> --------------------------------------- --------------------------------------- ? <= <= sub menu items, only ? but no content --------------------------------------- ? <= <= sub menu items, only ? but no content --------------------------------------- --------------------------------------- People --------------------------------------- <a tabindex="-1" title="People" href="/share/page/people-finder" class="alfresco-navigation-_HtmlAnchorMixin">People</a> --------------------------------------- [a,b,c,d]

我希望使用公共元素[b,d,f,h]获取数组。

我可以通过[b,d]filter

的组合实现这一目标
indexOf

但我想知道我是否以及如何使用[a,b,c,d].filter(el => [b,d,f,h].indexOf(el) !== -1) 来做同样的事情。

我承认,尽管看了很多例子,reduce仍然是我最晦涩的JS方法之一,所以我真的很感激一些建议。

3 个答案:

答案 0 :(得分:5)

ES6,一个Array#includes

的propoosal
  

includes() 方法确定数组是否包含某个元素,并根据需要返回truefalse

aa的每个循环上,如果在测试数组bb中找到值,则reduce会将元素添加到结果数组中。如果未找到,则返回前一个结果。

var aa = ['a','b','c','d'],
    bb = ['b','d','f','h'],
    cc = aa.reduce((r, a) => bb.includes(a) && r.concat(a) || r, []);

console.log(cc);

使用包含所有数组的单个数组只是一种更智能的方法。

var aa = ['a','b','c','d'],
    bb = ['b','d','f','h'],
    result = [aa, bb].reduce((a, b) => a.filter(c => b.includes(c)));

console.log(result);

答案 1 :(得分:2)

Reduce旨在从项目列表中返回单个值。所以过滤器在这里更有意义。

减少的一个好用途是返回公共元素的总数。请在此处查看:https://jsfiddle.net/c69vgzL4/

var a = ['a','b','c','d']
var b = ['b','d','f','h']

var number_of_common = b.reduce(function(prev, next) {
    return prev + (a.indexOf(next) + 1 ? 1 : 0)
}, 0)

$('body').html(number_of_common)

答案 2 :(得分:1)

不仅有两个数组,而是n个数组的交集...让我们发明Array.prototype.intersect()

&#13;
&#13;
Array.prototype.intersect = function(...a) {
  return [this,...a].reduce((p,c) => p.filter(e => c.includes(e)));
}

var arrs = [[0,2,4,6,8],[4,5,6,7],[4,6]],
     arr = [0,1,2,3,4,5,6,7,8,9];

console.log(JSON.stringify(arr.intersect(...arrs)));

// or just do

console.log(JSON.stringify(["a","b","c","d"].intersect(["b","d","f","h"])));
&#13;
&#13;
&#13;