I have two collections (Menu and Orders)
Menu collection contains array of Item objects
[{'id': '1', 'name': 'apple'}, {'id': '2', 'name': 'orange'}]
And Orders collection also contains array of Item objects
[{'id': '1', 'quantity': '0'}]
And I want them merged together their attributes by ID into another collection (this is needed for templating purposes only):
[{'id': '1', 'name': 'apple', 'quantity': '1'}, {'id': '2', 'name': 'orange'}]
Is there a underscore method on this or I need to define a function for this? [it seems i tried all merging functions on underscore but none of them works the way I expected]
Assuming your collections are Backbone collections
var menus = new Backbone.Collection([
{'id': '1', 'name': 'apple'},
{'id': '2', 'name': 'orange'}
]);
var orders = new Backbone.Collection([{'id': '1', 'quantity': 1}]);
you can take advantage of the functions proxied on collections and of collection.get
:
var merged = menus.map(function(menu) {
var id = menu.get('id'),
order = orders.get(id);
if (!order) return menu.toJSON();
return _.extend(menu.toJSON(), order.toJSON());
});
And a Fiddle to play with http://jsfiddle.net/bs6jN/
Don't think there is a function defined in underscore.js for this but one way to do it is by using the _.map and _.find functions as follows,
var menus = [{'id': '1', 'name': 'apple'}, {'id': '2', 'name': 'orange'}];
var orders = [{'id': '1', 'quantity': '0'}];
var newMenu = _.map(menus, function (menu) {
var order = _.find(orders, function (o) {
return o.id == menu.id;
});
return _.extend(menu, order);
});
console.log(newMenu);
This should do the job:
var result = [];
_.each(menu, function(el){
_.extend(el,_.where(orders, {id: el.id})[0] || {});
result.push(el);
});
If it is an union you want to do, you can do it using the underscore way:
// collectionUnion(*arrays, iteratee)
function collectionUnion() {
var args = Array.prototype.slice.call(arguments);
var it = args.pop();
return _.uniq(_.flatten(args, true), it);
}
It just an improvment of the original function _.union(*arrays)
, adding an iteratee to work collection (array of object).
Here how to use it:
var result = collectionUnion(a, b, c, function (item) {
return item.id;
});
The original function which is just working with array, looks like that:
_.union = function() {
return _.uniq(flatten(arguments, true, true));
};
And in bonus a full example:
// collectionUnion(*arrays, iteratee)
function collectionUnion() {
var args = Array.prototype.slice.call(arguments);
var it = args.pop();
return _.uniq(_.flatten(args, true), it);
}
var a = [{id: 0}, {id: 1}, {id: 2}];
var b = [{id: 2}, {id: 3}];
var c = [{id: 0}, {id: 1}, {id: 2}];
var result = collectionUnion(a, b, c, function (item) {
return item.id;
});
console.log(result); // [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 } ]
EDIT: I wrote a blog post if you want more details: http://geekcoder.org/union-of-two-collections-using-underscorejs/
We ca merge with plain js like this
var arrOne = [{'id': '1', 'name': 'apple'}, {'id': '2', 'name': 'orange'}];
var arrTwo = [{'id': '1','quantity': '0'}];
function mergeArr(arrOne, arrTwo) {
var mergedArr = [];
arrOne.forEach(function (item) {
var O = item;
arrTwo.forEach(function (itemTwo) {
if (O.id === itemTwo.id) {
O.quantity = itemTwo.quantity;
}
});
mergedArr.push(O);
});
return mergedArr;
}