0

First of all I'm not entirely sure if this is the right approach for the problem I am having but here it goes.

I have two arrays

const projects = ["PP", "JJ"]
const issues = ["10000", "10002", "100003"]

For each issue I want to create an object like so:

{
  "issueTypeId": "10002",
  "projectId": "PP",
  "viewType": "GIC"
}

And at the end finish with an array of objects, something like:

[{
   "issueTypeId": "10002",
   "projectId": "PP",
   "viewType": "GIC"
},
{
   "issueTypeId": "10000",
   "projectId": "PP",
   "viewType": "GIC"
}]

So far this is my logic

const projects = ["PP", "JJ"]
const issues = ["10000", "10002", "100003"]

const p = issues.map(issue => {
    return projects.map(project => {
        return {issueTypeId: issue, projectId: project, viewType: "GIC"}
    })
})

console.log(p)

But as you can see I get an array of arrays at the end which I'm pretty sure is due to the double .map logic.

1st - Is there a better way to achieve my objective?

2nd - If not, how to remove the inner array and get a single one

Thank you

Jorge Guerreiro
  • 682
  • 6
  • 22
  • 3
    `issues.map` -> `issues.flatMap` – VLAZ Apr 27 '22 at 11:41
  • ok that helps a lot, thanks! Is there a neater way of doing this, spreading? – Jorge Guerreiro Apr 27 '22 at 11:43
  • How do you define "neater"? – VLAZ Apr 27 '22 at 11:45
  • Maybe not the right choice of word but without nested logic – Jorge Guerreiro Apr 27 '22 at 11:48
  • "Is there a neater way of doing this, spreading": Probably not as long as you use `map`, as you can return only exactly one value there, not multiple ones. You could of course use two loops instead. – Remirror Apr 27 '22 at 11:49
  • In the end, you need to generate all combinations between the two arrays. Even if you don't have "nested logic" the cross product will still be the same amount of items: all of `issues` multiplied by all of `projects`. You can produce these [in different ways](https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript) but I'm not sure which you'd class as "better". If any. – VLAZ Apr 27 '22 at 11:51
  • I appreciate the help, I will have a read through the post you just shared, many thanks – Jorge Guerreiro Apr 27 '22 at 11:52

2 Answers2

1

Use flatMap instead of map. Plus, by choosing your variables wisely and using abbreviated form of arrow functions you could use the following:

const projects = ["PP", "JJ"],
      issues = ["10000", "10002", "100003"],
      viewType = "GIC",

      p = issues.flatMap(issueTypeId =>
          projects.map(projectId => ({issueTypeId, projectId, viewType}))
      );

console.log(p)
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

You can try this

const projects = ['PP', 'JJ'];
const issues = ['10000', '10002', '100003'];

let outArray = [];
issues.forEach((i) => {
  projects.forEach((p) => {
    outArray.push({
      issueTypeId: i,
      projectId: p,
      viewType: 'GIC',
    });
  });
});

console.log(outArray);
Vasim Hayat
  • 909
  • 11
  • 16