Reputation: 1
What I expect from this code is [1, 2, 5, 6, 7, 8, 9]
. The result is [7, 8, 9]
. Where is my mistake? Jsfiddle for this. Thanks.
var ar = [1, 2, 3, 4, 5, 6],
arWrapper = _(ar),
removedTypes = [3, 4],
addedTypes = [7, 8, 9];
_.each(removedTypes, function (removedType) {
arWrapper = arWrapper.remove(function (type) {
return type === removedType;
});
});
_.each(addedTypes, function (addedType) {
arWrapper = arWrapper.push(addedType);
});
console.log("%O", arWrapper.value());
Upvotes: 0
Views: 302
Reputation: 254926
Your solution is not "idiomatic", since it relies on free variables here and there (and is "too imperative" overall):
The "better" solution would be:
var result = arWrapper.difference(removedTypes).union(addedTypes);
JSFiddle: http://jsfiddle.net/xb90agw2/1/
As of your solution - it does not work because .remove()
returns a collection of removed elements, not the collection with filtered out elements as you assume.
Upvotes: 2