Alon
Alon

Reputation: 3906

Merging two backbone.js collections (outer join)

I have two large JSON vars with lots of data in it which each represents a collection.

Since both have similar attributes for each object inside each JSON, I would like to make an outer join, thus same attributes will stay only adding attributes which aren't in both (no duplicates)

var json1 = JSON.parse([{ name: "test_user1", id: 0102 }]
var json2 = JSON.parse([{ name: "test_user2", email: "[email protected]"}]

Merging both as I want should produce:

 [{ name: "test_user2", email: "[email protected]", id: 0102}]

Think as json1 and json2 has both the same number of objects inside it I would like to merge obj1 and obj1 from json1 and json2 together , obj2 and obj2 from json1 and json2 , etc...

Upvotes: 0

Views: 77

Answers (1)

Casey Foster
Casey Foster

Reputation: 6020

I initially misread your problem, but using at should let you merge by index position.

collectionA.each(function (model, index) {
  model.set(collectionB.at(index).attributes);
});

Upvotes: 1

Related Questions