如何在 lodash 中链接 Map 和 forEach?
How to chain Map and forEach in lodash?
我正在尝试在两个不同的数据源上使用 map 和 forEach 创建一个有效负载数组。有效负载数组应如下所示:
const payloads = [{
accountId: 1,
tagId: 'tag1',
notes: 'random note'
},
{
accountId: 1,
tagId: 'tag2',
notes: 'random note'
},
{
accountId: 1,
tagId: 'tag3',
notes: 'random note'
},
{
accountId: 2,
tagId: 'tag1',
notes: 'random note'
},
...]
我有以下变量:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const notes = 'random note'
我想使用此数据创建一个有效负载数组,以便每个 ID 都有一个单独的有效负载和每个注释。
我已经尝试使用 lodash map 和 forEach 执行以下操作:
import { forEach, map } from 'lodash';
const payloads = map(ids, id => forEach(tags, tag => {
return ({
accountId: id,
tagId: tag,
notes: note
}
)}));
这只是返回一组标签。我不确定哪里出错了,但我认为我没有正确理解链接。我在这里做错了什么?
两次都尝试使用 map
而不是 forEach
:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const note = 'random note'
const results = _.flatten(_.map(ids, id => _.map(tags, tag => ({
accountId: id,
tagId: tag,
notes: note
}))));
console.log(results)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
普通 JavaScript:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const note = 'random note'
const results = ids.map(id => tags.map(tag => ({
accountId: id,
tagId: tag,
notes: note
}))).flat();
console.log(results)
首先,lodash 的 forEach
始终 returns 输入数组原样。因此,对于每个 map
操作,您在概念上返回 tags
数组而不进行任何转换。你需要的是亨利回答的另一个地图操作员。但是嵌套的 map
也会导致嵌套数组。因此,结果不是您需要的结果,而是
[
[ {Object}, {Object}, {Object} ],
[ {Object}, {Object}, {Object} ],
[ {Object}, {Object}, {Object} ]
]
为了处理嵌套,需要对转换后的结果使用Array.prototype.flat
。
所以你的代码看起来像
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const notes = 'random note'
const payloads = _.map(ids, id => _.map(tags, tag => {
return ({
accountId: id,
tagId: tag,
notes: notes
})})).flat();
console.log(payloads);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
我正在尝试在两个不同的数据源上使用 map 和 forEach 创建一个有效负载数组。有效负载数组应如下所示:
const payloads = [{
accountId: 1,
tagId: 'tag1',
notes: 'random note'
},
{
accountId: 1,
tagId: 'tag2',
notes: 'random note'
},
{
accountId: 1,
tagId: 'tag3',
notes: 'random note'
},
{
accountId: 2,
tagId: 'tag1',
notes: 'random note'
},
...]
我有以下变量:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const notes = 'random note'
我想使用此数据创建一个有效负载数组,以便每个 ID 都有一个单独的有效负载和每个注释。
我已经尝试使用 lodash map 和 forEach 执行以下操作:
import { forEach, map } from 'lodash';
const payloads = map(ids, id => forEach(tags, tag => {
return ({
accountId: id,
tagId: tag,
notes: note
}
)}));
这只是返回一组标签。我不确定哪里出错了,但我认为我没有正确理解链接。我在这里做错了什么?
两次都尝试使用 map
而不是 forEach
:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const note = 'random note'
const results = _.flatten(_.map(ids, id => _.map(tags, tag => ({
accountId: id,
tagId: tag,
notes: note
}))));
console.log(results)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
普通 JavaScript:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const note = 'random note'
const results = ids.map(id => tags.map(tag => ({
accountId: id,
tagId: tag,
notes: note
}))).flat();
console.log(results)
首先,lodash 的 forEach
始终 returns 输入数组原样。因此,对于每个 map
操作,您在概念上返回 tags
数组而不进行任何转换。你需要的是亨利回答的另一个地图操作员。但是嵌套的 map
也会导致嵌套数组。因此,结果不是您需要的结果,而是
[
[ {Object}, {Object}, {Object} ],
[ {Object}, {Object}, {Object} ],
[ {Object}, {Object}, {Object} ]
]
为了处理嵌套,需要对转换后的结果使用Array.prototype.flat
。
所以你的代码看起来像
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const notes = 'random note'
const payloads = _.map(ids, id => _.map(tags, tag => {
return ({
accountId: id,
tagId: tag,
notes: notes
})})).flat();
console.log(payloads);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>