0

input string: "abc def, ghi jkl, mnopq"
Desired Output Arrays: ["abc","def"] ["ghi", "jkl"] ["mnopq"]

The input can be any combination of phrases separated by spaces, those phrases are then separated by commas. I need to make a new array for each input string which is followed by a comma. When those arrays are created they have to be split by " ".

Below is the code to split the string into array values using a comma as a separator:

str = "abc def, ghi jkl, mnopq";     
const commaSeparatedArray = this.str.split(',').filter(s => s.slice(-1) !== ' ');

console.log(commaSeparatedArray);

Not sure if the next step to take here is a for or while loop for such actions or a javascript prototype?

Link to stackblitz: https://stackblitz.com/edit/angular-ivy-vbdae5?file=src%2Fapp%2Fapp.component.ts

5 Answers 5

5

The following gives you an array of array. If something similar you are looking for.

var s = "abc def, ghi jkl, mnopq";
var result = s.split(',').map(a=>a.trim().split(' '));
console.log(result);

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

3 Comments

Nice idea with trim() 👍
@PramodMali Thanks, great stuff. If you don't mind my asking how would i iterate through both inner and outer arrays say to console log each string seperately. I have tried: result.forEach(a=>console.log(a));
@rkras, you can flatten the result array using Array.prototype.flat() and loop over. i.e result.flat().forEach(a=>console.log(a). For reference on flat() developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2

str = "abc def, ghi jkl, mnopq";

//you need to again split and filter out the empty string to get the desired output.

const commaSeparatedArray = this.str.split(',').map(item => {
  const d = item.split(' ').filter(i => i);
  return [...d]
})
console.log(commaSeparatedArray)

2 Comments

What's the filter for?
string has an extra space so when we use split it is giving an empty string and to avoid that I have used filter.
1

You basically just want to do two rounds of splitting

const str = "abc def, ghi jkl, mnopq";

const result = str.split(/,\s*/).map(substr => substr.split(/\s+/))

console.info(result)

That is

  1. Split the string on comma + zero-or-more spaces
  2. Then map each entry to another array by splitting that string on whitespace

Comments

1

const str = "abc def, ghi jkl, mnopq";

const commaSeparatedArray = str.split(",");

const result = commaSeparatedArray.reduce((acc, stringWithspaces) => {
  return [...acc, stringWithspaces.split(" ").filter(string=> string)];
}, []);

console.log(result);

Comments

1

I hope I have been helpful

var str = "abc def, ghi jkl, mnopq";
var res = str.split(', ').map(x => x.split(' '));
console.log(res);

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.