1

I have Array of objects like this:

[ { season: 1,
    episodes:
     [ 
       {
        title:'ep1 title',
        season:1,
       },
       {
        title:'ep2 title',
        season:1,
       },
       {
        title:'ep3 title',
        season:1,
       }] },
  { season: 2,
    episodes:
     [ {
        title:'ep1 title',
        season:2,
       },
       {
        title:'ep2 title',
        season:2,
       },
       {
        title:'ep3 title',
        season:2,
       } ] },
  { season: 3,
    episodes:
     [ {
        title:'ep1 title',
        season:3,
       },
       {
        title:'ep2 title',
        season:3,
       },
       {
        title:'ep3 title',
        season:3,
       } ] },
]

And I want turn it to array like this with loadsh:

[
  {
    title:'ep1 title',
    season:1,
  },
  {
    title:'ep2 title',
    season:1,
  },
  {
    title:'ep3 title',
    season:1,
  },
  {
    title:'ep1 title',
    season:2,
  },
  {
    title:'ep2 title',
    season:2,
  },
  {
    title:'ep3 title',
    season:2,
  },
  {
    title:'ep1 title',
    season:3,
  },
  {
    title:'ep2 title',
    season:3,
  },
  {
    title:'ep3 title',
    season:3,
  }
]

I just want to get episodes transform object to array with lodash how to merge two arrays into a object using lodash lodash: count values from array of objects

3
  • Not certain what Question is? Can you include the javascript that you have tried at OP? Commented Apr 19, 2017 at 19:12
  • By the way the output is still an array of objects. The subject or your question might be misleading. Commented Apr 19, 2017 at 19:23
  • @mfirry I know my mistake, What do you suggest for title? Commented Apr 19, 2017 at 19:25

2 Answers 2

6

If it really has to be lodash, you can use _.flatMap:

var flat = _.flatMap(data, 'episodes');

var data = [ { season: 1, episodes:[{title:'ep1 title',season:1,},{title:'ep2 title',season:1,},{title:'ep3 title',season:1,}] },{ season: 2,episodes:[ {title:'ep1 title',season:2,},{title:'ep2 title',season:2,},{title:'ep3 title',season:2,} ] },{ season: 3,episodes:[ {title:'ep1 title',season:3,},{title:'ep2 title',season:3,},{title:'ep3 title',season:3,} ] },];

var flat = _.flatMap(data, 'episodes');

console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

But JavaScript has since EcmaScript 2019 its own flatMap, so you can just do:

var flat = data.flatMap( o => o.episodes );

var data = [ { season: 1, episodes:[{title:'ep1 title',season:1,},{title:'ep2 title',season:1,},{title:'ep3 title',season:1,}] },{ season: 2,episodes:[ {title:'ep1 title',season:2,},{title:'ep2 title',season:2,},{title:'ep3 title',season:2,} ] },{ season: 3,episodes:[ {title:'ep1 title',season:3,},{title:'ep2 title',season:3,},{title:'ep3 title',season:3,} ] },];

var flat = data.flatMap( o => o.episodes );
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If flatMap is not available, you can use:

var flat = [].concat(...data.map( o => o.episodes ));

var data = [ { season: 1, episodes:[{title:'ep1 title',season:1,},{title:'ep2 title',season:1,},{title:'ep3 title',season:1,}] },{ season: 2,episodes:[ {title:'ep1 title',season:2,},{title:'ep2 title',season:2,},{title:'ep3 title',season:2,} ] },{ season: 3,episodes:[ {title:'ep1 title',season:3,},{title:'ep2 title',season:3,},{title:'ep3 title',season:3,} ] },];

var flat = [].concat(...data.map( o => o.episodes ));
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

No need to use any special lodash functions - this can be done very easily with basic javascript array .reduce() like so:

var oldArr = [{
    season: 1,
    episodes: [{
        title: 'ep1 title',
        season: 1,
      },
      {
        title: 'ep2 title',
        season: 1,
      },
      {
        title: 'ep3 title',
        season: 1,
      }
    ]
  },
  {
    season: 2,
    episodes: [{
        title: 'ep1 title',
        season: 2,
      },
      {
        title: 'ep2 title',
        season: 2,
      },
      {
        title: 'ep3 title',
        season: 2,
      }
    ]
  },
  {
    season: 3,
    episodes: [{
        title: 'ep1 title',
        season: 3,
      },
      {
        title: 'ep2 title',
        season: 3,
      },
      {
        title: 'ep3 title',
        season: 3,
      }
    ]
  },
]

var newArr = oldArr.reduce(function(result, current) {
  return result.concat(current.episodes);
}, []);

console.log(newArr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.