javascript中数组交集的最简单代码

javascript中数组交集的最简单代码

在javascript中实现数组交叉的最简单,无库的代码是什么?我想写

intersection([1,2,3], [2,3,4,5])

得到

[2, 3]


largeQ
浏览 744回答 3
3回答

收到一只叮咚

使用的组合Array.prototype.filter和Array.prototype.indexOf:array1.filter(value => -1 !== array2.indexOf(value))或者正如vrugtehagel在评论中所建议的那样,你可以使用更新的更Array.prototype.includes简单的代码:array1.filter(value => array2.includes(value))对于旧浏览器:array1.filter(function(n) {     return array2.indexOf(n) !== -1;});

慕盖茨4494581

破坏性似乎最简单,特别是如果我们可以假设输入已排序:/*&nbsp;destructively&nbsp;finds&nbsp;the&nbsp;intersection&nbsp;of&nbsp; &nbsp;*&nbsp;two&nbsp;arrays&nbsp;in&nbsp;a&nbsp;simple&nbsp;fashion.&nbsp;&nbsp; &nbsp;* &nbsp;*&nbsp;PARAMS &nbsp;*&nbsp;&nbsp;a&nbsp;-&nbsp;first&nbsp;array,&nbsp;must&nbsp;already&nbsp;be&nbsp;sorted &nbsp;*&nbsp;&nbsp;b&nbsp;-&nbsp;second&nbsp;array,&nbsp;must&nbsp;already&nbsp;be&nbsp;sorted &nbsp;* &nbsp;*&nbsp;NOTES &nbsp;*&nbsp;&nbsp;State&nbsp;of&nbsp;input&nbsp;arrays&nbsp;is&nbsp;undefined&nbsp;when &nbsp;*&nbsp;&nbsp;the&nbsp;function&nbsp;returns.&nbsp;&nbsp;They&nbsp;should&nbsp;be&nbsp; &nbsp;*&nbsp;&nbsp;(prolly)&nbsp;be&nbsp;dumped. &nbsp;* &nbsp;*&nbsp;&nbsp;Should&nbsp;have&nbsp;O(n)&nbsp;operations,&nbsp;where&nbsp;n&nbsp;is&nbsp; &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;MIN(a.length,&nbsp;b.length) &nbsp;*/function&nbsp;intersection_destructive(a,&nbsp;b){ &nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;[]; &nbsp;&nbsp;while(&nbsp;a.length&nbsp;>&nbsp;0&nbsp;&&&nbsp;b.length&nbsp;>&nbsp;0&nbsp;) &nbsp;&nbsp;{&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a[0]&nbsp;<&nbsp;b[0]&nbsp;){&nbsp;a.shift();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(a[0]&nbsp;>&nbsp;b[0]&nbsp;){&nbsp;b.shift();&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;/*&nbsp;they're&nbsp;equal&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.push(a.shift()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.shift(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;result;}非破坏性必须是一个更复杂的头发,因为我们必须跟踪索引:/*&nbsp;finds&nbsp;the&nbsp;intersection&nbsp;of&nbsp; &nbsp;*&nbsp;two&nbsp;arrays&nbsp;in&nbsp;a&nbsp;simple&nbsp;fashion.&nbsp;&nbsp; &nbsp;* &nbsp;*&nbsp;PARAMS &nbsp;*&nbsp;&nbsp;a&nbsp;-&nbsp;first&nbsp;array,&nbsp;must&nbsp;already&nbsp;be&nbsp;sorted &nbsp;*&nbsp;&nbsp;b&nbsp;-&nbsp;second&nbsp;array,&nbsp;must&nbsp;already&nbsp;be&nbsp;sorted &nbsp;* &nbsp;*&nbsp;NOTES &nbsp;* &nbsp;*&nbsp;&nbsp;Should&nbsp;have&nbsp;O(n)&nbsp;operations,&nbsp;where&nbsp;n&nbsp;is&nbsp; &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;MIN(a.length(),&nbsp;b.length()) &nbsp;*/function&nbsp;intersect_safe(a,&nbsp;b){ &nbsp;&nbsp;var&nbsp;ai=0,&nbsp;bi=0; &nbsp;&nbsp;var&nbsp;result&nbsp;=&nbsp;[]; &nbsp;&nbsp;while(&nbsp;ai&nbsp;<&nbsp;a.length&nbsp;&&&nbsp;bi&nbsp;<&nbsp;b.length&nbsp;) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a[ai]&nbsp;<&nbsp;b[bi]&nbsp;){&nbsp;ai++;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(a[ai]&nbsp;>&nbsp;b[bi]&nbsp;){&nbsp;bi++;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;/*&nbsp;they're&nbsp;equal&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.push(a[ai]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ai++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bi++; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;result;}

翻过高山走不出你

如果您的环境支持ECMAScript 6 Set,那么一种简单且有效的(参见规范链接)方式:function&nbsp;intersect(a,&nbsp;b)&nbsp;{ &nbsp;&nbsp;var&nbsp;setA&nbsp;=&nbsp;new&nbsp;Set(a); &nbsp;&nbsp;var&nbsp;setB&nbsp;=&nbsp;new&nbsp;Set(b); &nbsp;&nbsp;var&nbsp;intersection&nbsp;=&nbsp;new&nbsp;Set([...setA].filter(x&nbsp;=>&nbsp;setB.has(x))); &nbsp;&nbsp;return&nbsp;Array.from(intersection);}更短,但可读性更低(也没有创建额外的交集Set):function&nbsp;intersect(a,&nbsp;b)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[...new&nbsp;Set(a)].filter(x&nbsp;=>&nbsp;new&nbsp;Set(b).has(x));}避免新Set的b每次:function&nbsp;intersect(a,&nbsp;b)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;setB&nbsp;=&nbsp;new&nbsp;Set(b); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[...new&nbsp;Set(a)].filter(x&nbsp;=>&nbsp;setB.has(x));}请注意,使用集合时,您将只获得不同的值,因此new Set[1,2,3,3].size计算结果为3。
打开App,查看更多内容
随时随地看视频慕课网APP