javascript - underscore/lodash - combine objects with same elements -
I'm trying to lack better words 'zip' or 'merge' json objects in each single single reacting element on the basis of . Example:
var obj = {CATS: [{id: 123, LIST: [{Result 1: CAT1}, {result 2: cat 2}}}, {ID: 987, List: [Results 1: CAT3}, {Results 2: CAT4}]}, DOGS: [{id: 123, LIST: [{Result 1: DOG1}, {result 2: DOG2}}}, {id: 987, LIST: [result1: DOG3}, {result2: DOG4}]}]} I would like to use nested 'id' within obj to create:
var New_obj = {123: [CATS: {LIST: [{RESULT 1: CAT1}, {RESULT 2: CAT2}}}, DOGS: {LIST: [{Result 1: DOG1}, {Result 2: DOG2}]}, 987: [Cats: {LIST: [{result1: CAT3}, {result2: CAT4}}}, DOGS: {LIST: [{result1: DOG3}, {result2: DOG4}]} }} for LoDash or underscores Part ... whatever works thanks!
** WARNING! IO 8 should be compatible. Bleh! **
Wow, after some testing and error, what do I need for you ... < / P>
var results = _.readuce (obj, function (memorandum, group, key) {var ids = _.map (group, function (g) {return g.id;}); _ Each (id, function (id) {var obj = {}; obj [key] = {}; obj [key] .LIST = _.filter (group, function (g) {return g.id === id; }} .map (function (g) {return g. Lith;}) [0]; memo [id] = (memo [id] || []), memo [id]. Push (obz);}); Return memo;}, {}); make it work Do it ...
Comments
Post a Comment