Underscore.js - 没有和差异方法麻烦 [英] Underscore.js - without and difference methods trouble
问题描述
我正在尝试使用 underscore.js 库中的 _.without 和 _.difference 方法,但没有成功.
I am trying to use the _.without and _.difference methods in the underscore.js library with no luck.
这是一些代码.
var someIds = _.pluck(parsedID1, 'id');
var otherIds = _.pluck(parsedID2, 'id);
这里是麻烦开始的地方,我想从 someId 的列表中删除所有 otherId.所以我根据网站有两个选择,要么我可以使用 _.difference ||_.没有.我像这样使用它们...
Here is where the trouble starts, I want to remove all of the otherIds out of the someId's list. So I have two options according to the website, either I can use _.difference || _.without. I use them like so...
var newArray = _.without(_.toArray(someIds), _.toArray(otherIds));
我也尝试使用 _.difference 来执行此操作,但除了将第一个参数的副本返回到函数中之外,我似乎无法获得其他方法.我也试过将参数传递给函数而不使用 _.toArray() 但同样的事情发生了.以前有没有人遇到过这个错误?
I have also tried to do this with _.difference as well, but I cant seem to get the methods to do anything other than return a copy of the first argument into the function. I have also tried passing the arguments into the functions without using the _.toArray() but the same thing happens.Has anyone ever encountered this error before?
我已经尝试了下面建议的方法.我会给出更多的代码,希望能解决这个问题.
I have tried the way as proposed below. I will give some more code in hopes of fixing this.
我作为参数传入的数组采用以下格式.
The arrays that I am passing in as arguments are in the following format.
var array = ['100004191488120', '100004191488321'];
当我传入 _.difference 时,我执行以下操作:
When I pass into the _.difference I do as follows:
var newArray = _.difference(someIds, otherIds);
如果 someIds 长度为 657 而 otherIds 为 57 我应该得到 600 Id 的结果.相反,无论第一个参数的长度是多少,我都会得到一个结果,即 657.
If someIds length is 657 and otherIds is 57 I should be getting a result of 600 Id's. Instead I get a result of whatever the length of the first argument is, being 657.
编辑 2:已解决
我想出了问题所在,在经历了这么多挫折之后,问题在于我传入的第二个差异论点.
I figured out what the problem was, after all that frustration the problem lied with the second argument to difference I was passing in.
我正在做一个数据库查询并返回一个如下所示的列表.
I was doing a db query and getting back a list that looked like the following.
[{ _id: 514721152222111116666777, facebook: { id: '123456789' } },
{ _id: 514721152222111116666778, facebook: { id: '12345678' } },]
在我收到这些结果后,我想获得 facebook id,所以我做了以下操作,
After I received those results I wanted to get the facebook id so I did the following,
var array = _.pluck(users, 'facebook.id');
这就是问题所在,那个 pluck 给我发回了一个未定义的列表.相反,我之前不得不做 2 次弹拨,而不是一次,如下所示.
Thats were the problem was, that pluck was sending me back a list of undefined. Instead I had to do 2 plucks before instead of one, like the following.
var temp = _.pluck(users, 'facebook');
var arrayId = _.pluck(temp, 'id');
感谢@ZenMaster 放弃了关于 _.without && 之间区别的知识._.区别.你的回答是正确的,在一个活页夹之后脑子已经死了,没有看到明显的东西.
Thanks @ZenMaster for dropping the knowledge on the difference between _.without && _.difference. Your answer was correct, was pretty brain-dead after a binder and didn't see the obvious.
推荐答案
without
函数的第一个参数不需要_.toArray
without
的第二个(以及更多)参数不是数组 - 它是值的列表".
- You don't need an
_.toArray
on the first argument of thewithout
function - The second (and more) arguments to the
without
is not an array - it's a "list" of values.
正确的调用应该是:
var newArray = _.without(['1', '2', '3'], '1', '3');
这将导致:
['2']
在您的情况下,您必须使用 _.difference
,如 JSFiddle 在这里.
In your case, you'd have to use _.difference
as can be seen on JSFiddle here.
代码如下:
var parsedID1 = [{id: '1', name: 'name1'}, {id: '2', name: 'name2'}];
var parsedID2 = [{id: '1', name: 'name1'}, {id: '3', name: 'name3'}];
var someIds = _.pluck(parsedID1, 'id');
var otherIds = _.pluck(parsedID2, 'id');
console.log(someIds, otherIds);
var newArray = _.difference(someIds, otherIds);
console.log(newArray);
这篇关于Underscore.js - 没有和差异方法麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!