什么是计算使用Javascript数组一组不同的最快或最优雅的方式? [英] What is the fastest or most elegant way to compute a set difference using Javascript arrays?

查看:152
本文介绍了什么是计算使用Javascript数组一组不同的最快或最优雅的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A B 两套。我在寻找的真正的快速或优雅的方式来计算差集( A - B A \\ B ,它们之间的取决于你的preference)。两组的存储和操作类似于JavaScript数组,正如标题所说。

Let A and B be two sets. I'm looking for really fast or elegant ways to compute the set difference (A - B or A \B, depending on your preference) between them. The two sets are stored and manipulated as Javascript arrays, as the title says.

注:


  • 针对Gecko的招数都还好

  • 我preFER坚持原生功能(但我开到一个轻量级库,如果它的方式更快)

  • 我见过,但没有测试过, JS.Set (见previous点)

  • Gecko-specific tricks are okay
  • I'd prefer sticking to native functions (but I am open to a lightweight library if it's way faster)
  • I've seen, but not tested, JS.Set (see previous point)

编辑:我注意到了有关含重复元素集的评论。当我说设置,我指的是数学上的定义,这意味着(除其他事项外),它们不包含重复的元素。

I noticed a comment about sets containing duplicate elements. When I say "set" I'm referring to the mathematical definition, which means (among other things) that they do not contain duplicate elements.

推荐答案

如果不知道这是否是最有效的,但也许是最短的。

if don't know if this is most effective, but perhaps the shortest

A = [1, 2, 3, 4];
B = [1, 3, 4, 7];

diff = A.filter(function(x) { return B.indexOf(x) < 0 })

这篇关于什么是计算使用Javascript数组一组不同的最快或最优雅的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆