[英]Lodash Transform array of objects to array
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:我想用 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我只是想使用 lodash 将剧集转换为对象如何使用 lodash 将两个数组合并为一个对象
If it really has to be lodash, you can use _.flatMap
:如果它真的必须是 lodash,你可以使用
_.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:但是 JavaScript 从 EcmaScript 2019 开始有了自己的
flatMap
,所以你可以这样做:
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:如果
flatMap
不可用,您可以使用:
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; }
No need to use any special lodash functions - this can be done very easily with basic javascript array .reduce()
like so:无需使用任何特殊的 lodash 函数 - 这可以使用基本的 javascript 数组
.reduce()
轻松完成,如下所示:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.