0

I have a situation where I need to split the already split string.

For example,

String 1= "This,is, my, problem;I, need,a, solution";

Now I need to split the first sentence based on ";" and once I get this string, which should give me 2 sentences

1 "This,is,my,problem"

2 "I,need,a,solution"

And I need to further split these two sentences based on ",".

This

is

my

problem

I

need

a

solution

Is this possible using JavaScript.When I am trying to split the two sentences, I am getting error in JavaScript.

Dinshaw Raje
  • 933
  • 1
  • 12
  • 33

1 Answers1

1

You need a combination of .map and .split.

var x = "This,is, my, problem;I, need,a, solution";

var op = x
        .split(";") // first split
        .map(el => el
                  .split(",") // second split
                  .map(el => el.trim())) // to remove the begining and end space
        .flat(1); // to flatten the array by 1 level

console.log(op);
Archie
  • 901
  • 4
  • 11