0
const rules = transposed
  .slice(1)
  .map(
    ([
      sourceSystem,
      classification1, classification2, classification3, classification4,
      segment,
      calendar,
      createSla,
      slaDurationInMinutes,
      sendNotification,
      useHolidays,
    ]: any) => ({
      calendar,
      // classification: 'ABC, DEF',
      classification1,
      classification2,
      classification3,
      classification4,
      createSla,
      segment,
      sendNotification,
      slaDurationInMinutes,
      sourceSystem,
    }),
  );

How can I join the string values of classification1...4 like

classification: classification1 + classification2 + classification3 + classification4?

The result should be classifications: "ABC,DEF,XYZ,PEF"

1
  • 3
    Your "like" example is almost literally how you would do it, unless there are other constraints you haven't mentioned (such as some of them being optional/possibly blank, etc.). What specific issue did you have doing that? Commented Jul 20, 2021 at 12:13

2 Answers 2

2

Your "like" example is almost literally how you would do it, unless there are other constraints you haven't mentioned (such as some of them being optional/possibly blank, etc.):

const rules = transposed
  .slice(1)
  .map(
    ([
      sourceSystem,
      classification1, classification2, classification3, classification4,
      segment,
      calendar,
      createSla,
      slaDurationInMinutes,
      sendNotification,
      useHolidays,
    ]: any) => ({
      calendar,
      classification: classification1 + "," + classification2 + "," + classification3 + "," + classification4, // ***
      createSla,
      segment,
      sendNotification,
      slaDurationInMinutes,
      sourceSystem,
    }),
  );

(Or with a template literal as shown by Nenad.)

If some of them may be blank and you want to skip those, you might create and filter an array, then use .join(",") to join the values:

const rules = transposed
  .slice(1)
  .map(
    ([
      sourceSystem,
      classification1, classification2, classification3, classification4,
      segment,
      calendar,
      createSla,
      slaDurationInMinutes,
      sendNotification,
      useHolidays,
    ]: any) => ({
      calendar,
      classification: [classification1, classification2, classification3, classification4].filter(c => !!c).join(",")
      createSla,
      segment,
      sendNotification,
      slaDurationInMinutes,
      sourceSystem,
    }),
  );

(Yes, .filter(c => !!c) could be just .filter(c => c) or .filter(Boolean). I write what seems clear to me and let the minifier worry about minifying. :-) )

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

Comments

2

Try simply with template literals:

const rules = transposed
  .slice(1)
  .map(
    ([
      sourceSystem,
      classification1, classification2, classification3, classification4,
      segment,
      calendar,
      createSla,
      slaDurationInMinutes,
      sendNotification,
      useHolidays,
    ]: any) => {
    return {
      calendar,
      classification: `${classification1},${classification2},${classification3},${classification4}`,
      createSla,
      segment,
      sendNotification,
      slaDurationInMinutes,
      sourceSystem
    }
  });

Comments

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.