When splitting input strings by commas using .split(','), I encountered an issue with strings that contain commas within a single name. For example: "John, Smith".
Typically, my strings appear like this: "Emily, Sasha Flora, Camille-O'neal", resulting in 3 objects as required. However, occasionally I receive strings such as "Emily, Sasha Flora, Camille-O'neal, John, Smith", leading to 5 objects instead of the expected 4.
How can I modify the code to handle strings containing commas while still obtaining the desired result?
The current code snippet is:
var myValue = ["Emily, Sasha Flora, John, Smith, Camille-O'neal"]
var names = myValue.split(',').map(item => ({ name: item.trim() }));
I attempted seeking assistance from ChatGPT without success.
The intended output should be:
Emily
Sasha Flora
John, Smith
Camille-O'neal