Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var obj = {
    CATS:[
        {
            id:123,
            LIST:[
                {result1:"CAT1"},
                {result2:"CAT2"}
            ]
        },
        {
            id:987,
            LIST:[
                {result1:"CAT3"},
                {result2:"CAT4"}
            ]
        }
    ],
    DOGS:[
        {
            id:123,
            LIST:[
                {result1:"DOG1"},
                {result2:"DOG2"}
            ]
        },
        {
            id:987,
            LIST:[
                {result1:"DOG3"},
                {result2:"DOG4"}
            ]
        }
    ]    
};
var result = _.reduce(obj, function(memo, group, key) {
  var ids = _.map(group, function(g) {
    return g.id;
  });
  
  _.each(ids, function(id) {
    var obj = {};
    
    obj[key] = {};
    obj[key].LIST = _.filter(group, function(g) {
      return g.id === id;
    }).map(function(g) {
      return g.LIST;
    })[0];
    
    memo[id] = (memo[id] || []);
    
    memo[id].push(obj);
  });
  
  return memo;
}, {});
console.log(JSON.stringify(result, null, 2));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jcreamer898pro
0viewers