Merge arrays that are on the same key - Javascript Array Operation

Javascript examples for Array Operation:Associative Array

Description

Merge arrays that are on the same key

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() {//from  w ww. j  av  a2s . c o  m
h1 = {12: [{actor: 'wayne'}, {actor: 'bill'}], 13: [{actor: 'james'}]}
    h2 = {13: [{actor: 'mark'}]}
for (var k in h2) {
   h1[k]=(h1[k]||[]).concat(h2[k]);
}
console.log(h1);
    });

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

Related Tutorials