I am trying to use the _.without and _.difference methods in the underscore.js library with no luck.
Here is some code.
var someIds = _.pluck(parsedID1, 'id');
var otherIds = _.pluck(parsedID2, 'id);
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));
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?
EDIT:
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'];
When I pass into the _.difference I do as follows:
var newArray = _.difference(someIds, otherIds);
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.
EDIT 2: Solved
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' } },]
After I received those results I wanted to get the facebook id so I did the following,
var array = _.pluck(users, 'facebook.id');
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');
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.
I am trying to use the _.without and _.difference methods in the underscore.js library with no luck.
Here is some code.
var someIds = _.pluck(parsedID1, 'id');
var otherIds = _.pluck(parsedID2, 'id);
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));
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?
EDIT:
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'];
When I pass into the _.difference I do as follows:
var newArray = _.difference(someIds, otherIds);
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.
EDIT 2: Solved
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' } },]
After I received those results I wanted to get the facebook id so I did the following,
var array = _.pluck(users, 'facebook.id');
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');
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.
Share Improve this question edited Mar 18, 2013 at 17:25 BJacobs asked Mar 18, 2013 at 4:04 BJacobsBJacobs 1222 silver badges8 bronze badges1 Answer
Reset to default 4- 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.
The proper call would be:
var newArray = _.without(['1', '2', '3'], '1', '3');
which would result in:
['2']
In your case, you'd have to use _.difference
as can be seen on JSFiddle here.
Here is the code:
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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745605910a4635672.html
评论列表(0条)