简体   繁体   中英

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: "someemail@gmail.com"}]

Merging both as I want should produce:

 [{ name: "test_user2", email: "someemail@gmail.com", 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...

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM