0

I have an array like below:

[
{productypeId: 1, producsubtypeId: 1, productid: 1},
{productypeId: 1, producsubtypeId: 1, productid: 2},
{productypeId: 1, producsubtypeId: 2, productid: 3},
{productypeId: 1, producsubtypeId: 2, productid: 4},
{productypeId: 2, producsubtypeId: 5, productid: 6},
{productypeId: 2, producsubtypeId: 5, productid: 7}
]

I want the following output:

[
{productypeId: 1, producsubtypeId: 1, d: '1,2'},
{productypeId: 1, producsubtypeId: 2, d: '3,4'},
{productypeId: 2, producsubtypeId: 5, d: '6,7'}
]

please help me on this Thanks in advance

5
  • Will productTypeId and productSubTypeId always be the same? I.e. 1. And if they are not the same, what do you want to hapoen? Commented Apr 11, 2020 at 11:13
  • I have changed It can you please see and help me Commented Apr 11, 2020 at 11:39
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a minimal reproducible example. Commented Apr 11, 2020 at 11:50
  • I am having that input array and covert the first array into second array in typescript and save in database I want the second mentioned array has output Commented Apr 11, 2020 at 11:53
  • Ok I understand what you are trying to do now. Please format your code example properly though: stackoverflow.com/help/formatting Commented Apr 11, 2020 at 11:59

1 Answer 1

1

What i see from your output, i assume you try to make a output which have same value of productypeId and producsubtypeId assign the productid as duplicate value. Here my solution with array reduce. Hope this could solve your issue.

interface InputItem {
  productypeId: number;
  producsubtypeId: number;
  productid: number;
}

interface OuputItem {
  productypeId: number;
  producsubtypeId: number;
  d: string;
}

const input: InputItem[] = [
  { productypeId: 1, producsubtypeId: 1, productid: 1 },
  { productypeId: 1, producsubtypeId: 1, productid: 2 },
  { productypeId: 1, producsubtypeId: 2, productid: 3 },
  { productypeId: 1, producsubtypeId: 2, productid: 4 },
  { productypeId: 2, producsubtypeId: 5, productid: 6 },
  { productypeId: 2, producsubtypeId: 5, productid: 7 }
];

const output = input.reduce((res, item) => {
  // destructuring input item
  const { productypeId, producsubtypeId, productid } = item;

  // find unique set by productypeId and producsubtypeId in accumulator 'res'
  const groupIndex = res.findIndex(
    i =>
      i.productypeId === productypeId && producsubtypeId === i.producsubtypeId
  );

  // if index exist in accumulator, we concat productid value as 'd'
  // else push a new unique set to accumulator
  if (groupIndex > -1) {
    res[groupIndex].d += `,${productid}`;
  } else {
    res.push({ productypeId, producsubtypeId, d: `${productid}` });
  }

  return res;
}, [] as OuputItem[]);

https://stackblitz.com/edit/typescript-2egw3w

Sign up to request clarification or add additional context in comments.

1 Comment

Please code your code here, not just on stackblitz.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.