如何使用下划线的“交集”在物体上?
_.intersection([], [])
只适用于原始类型,对吧?
它不适用于对象。我怎样才能让它与对象一起工作(也许通过检查“Id”字段)?
var a = [ {'id': 1, 'name': 'jake' }, {'id':4, 'name': 'jenny'} ]
var b = [ {'id': 1, 'name': 'jake' }, {'id': 9, 'name': 'nick'} ]
在这个例子中,结果应该是:
_.intersection(a, b);
<块引用> <块引用>[ {'id': 1, 'name': '杰克' } ];
_.intersection([], [])
only works with primitive types, right?
It doesn't work with objects. How can I make it work with objects (maybe by checking the "Id" field)?
var a = [ {'id': 1, 'name': 'jake' }, {'id':4, 'name': 'jenny'} ]
var b = [ {'id': 1, 'name': 'jake' }, {'id': 9, 'name': 'nick'} ]
In this example, the result should be:
_.intersection(a, b);
[ {'id': 1, 'name': 'jake' } ];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以基于下划线的函数创建另一个函数。您只需更改原始函数中的一行代码:
在本例中,您现在将使用 underscore 的 isEqual() 方法,而不是 JavaScript 的相等比较器。我用你的例子尝试了一下,效果很好。以下是 underscore 文档中有关 isEqual 函数的摘录:
您可以在此处找到文档: http:// documentcloud.github.com/underscore/#isEqual
我将代码放在 jsFiddle 上,以便您可以测试并确认它:http://jsfiddle.net/luisperezphd/jrJxT/
You can create another function based on underscore's function. You only have to change one line of code from the original function:
In this case you'd now be using underscore's isEqual() method instead of JavaScript's equality comparer. I tried it with your example and it worked. Here is an excerpt from underscore's documentation regarding the isEqual function:
You can find the documentation here: http://documentcloud.github.com/underscore/#isEqual
I put up the code on jsFiddle so you can test and confirm it: http://jsfiddle.net/luisperezphd/jrJxT/
这是一种灵活且性能更好的替代算法。其中一项改进是您可以指定自己的比较函数,因此在您的情况下,如果 id 是唯一标识符,您可以只比较 id。
您可以这样使用它:
或者您可以省略该函数,它将使用下划线 _.isEqual() 函数,如下所示:
您可以在 jsFiddle 上找到它: http://jsfiddle.net/luisperezphd/43vksdn6/
Here is an alternative algorithm that should be flexible and perform better. One of those improvements is that you can specify your own comparison function so in your case you can just compare the id if it's a unique identifier.
You can use it like this:
Or you can leave out the function and it will use underscores _.isEqual() function, like so:
You can find it on jsFiddle here: http://jsfiddle.net/luisperezphd/43vksdn6/
下划线中的数组方法非常强大,您应该只需要几行即可完成您想要做的事情:
如果您有大型数组,您可以通过额外一行来摆脱
compact()
调用:The array methods in underscore are very powerful, you should only need a few lines to accomplish what you want to do:
If you have large arrays you can get rid of the
compact()
call with one additional line:我想分享我针对这些情况的通用解决方案。
我使用 mixin 在下划线中添加了一个通用函数,它根据给定的哈希函数对两个集合执行二进制“数组”操作:
使用示例:
请注意,'id' 可以替换为功能。
我相信这个解决方案是 O(n+m)。
I'd like to share my general solution for those cases.
I added a general function to underscore, using mixin, which performs a binary 'array' operation on two collections, according to a given Hash function:
Usage example:
Note that 'id' may be replaced with a function.
I believe this solution is O(n+m).
在洛达什 4.0.0 中。我们可以这样尝试
输出:
[ {'id': 1, 'name': 'jake' } ];
In lodash 4.0.0. We can try like this
Output:
[ {'id': 1, 'name': 'jake' } ];
从技术上讲,它确实适用于对象,但您需要注意引用相等性。
Technically, it does work on objects, but you need to be careful of reference equality.
工作功能:
在Pointy的建议之后,我还专门尝试了jQuery中的代码。比较必须根据 JSON 对象的结构进行定制。
Working function:
I have also tried code in jQuery specially after Pointy's suggestion. Compare has to be customizable as per the structure of JSON object.
如果您只想比较对象:
If you wanna compare only objects:
我尝试用这种方法解决它。
I tried solving it this way.