flatten group Array and Objects - Javascript Array Operation

Javascript examples for Array Operation:Array of Object

Description

flatten group Array and Objects

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var arr = [/*from w  w  w.  ja  v a 2s .co  m*/
  { data: [{ artist: 'Artist' }, { artist: 'Artist2' }]},
  { data: { artist: 'Artist3' } },
  { data: [{ artist: 'Artist4' }]}
];
var flattened = [];
arr.forEach(function (el) {
  if(Array.isArray(el.data)) {
    flattened = flattened.concat(el.data);
  } else {
    flattened.push(el.data);
  }
});
console.log(flattened);

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

Related Tutorials