Merge two array of objects in javascript - Javascript Array Operation

Javascript examples for Array Operation:Array of Object

Description

Merge two array of objects in javascript

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/* w  ww.  j av a  2s.  c  om*/
    
    function addChildren(master, child){
        return master.map(function(elem, i){
            elem.child = elem.child || [];
            elem.child.push(child[i]);
            return elem;
        });
    };
    console.log(addChildren([{a:1},{b:2}],[{k:3},{h:2}]));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials