我有这样的对象数组:
[ { 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,
} ] },
]
Run Code Online (Sandbox Code Playgroud)
我想用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,
}
]
Run Code Online (Sandbox Code Playgroud)
我只想用lodash将episodes转换对象转换为数组如何使用lodash lodash将两个数组合并到一个对象中:从对象数组中计算值
您可以在以下位置使用纯JavaScript data:
var flat = [].concat(...data.map( o => o.episodes ));
Run Code Online (Sandbox Code Playgroud)
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);Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)
如果它真的必须是lodash,你可以使用_.flatMap:
var flat = _.flatMap(data, 'episodes');
Run Code Online (Sandbox Code Playgroud)
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);Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
951 次 |
| 最近记录: |