I'm trying to create an array of payloads using map and forEach on two different data sources. The payloads array should look like this:
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'
},
...]
I have the following variables:
const ids = [1, 2, 3]
const tags = ['tag1', 'tag2', 'tag3']
const notes = 'random note'
I'd like to create an array of payloads using this data so that each id has a separate payload with a each note.
I've tried doing the following with lodash map and forEach:
import { forEach, map } from 'lodash';
const payloads = map(ids, id => forEach(tags, tag => {
return ({
accountId: id,
tagId: tag,
notes: note
}
)}));
This is just returning an array of tags. I'm not sure where I'm going wrong, but I don't think I understand chaining correctly. What am I doing wrong here?