parse date and number from API results I'm slurping up fields from an API that returns an array of fields. Each field in the array is a String that actually contains two separate fields (a number and a date). The number is enclosed in parentheses and the date is following this and a space. The following is an example of the format.
const data = [
            &quot;(2) 2020-09-15&quot;, &quot;(3) 2020-09-16&quot;
        ];

I'm parsing this field and then storing the data separately in my app. I have my own numbers and dates fields that will be a String in which each number and date is separated by a newline.
I'm achieving this by doing the following.
let numbers = '', dates = numbers;

        for (let datum of data) {
            datum = datum.split(/[() ]/);
            numbers += `${datum[1]}\n`;
            dates += `${datum[3]}\n`;
        }

See an example here.
I don't particularly like this and am wondering if there's a more efficient and cleaner way to write this.
Rather than declaring the variables with let (you should always prefer const) and concatenating and reassigning, consider creating arrays of numbers and dates instead, eg:
[2, 3] and [&quot;2020-09-15&quot;, &quot;2020-09-16&quot;]
Then after the loop is done, join all elements by newlines.
For the regular expression, rather than split, I think match would be more appropriate. You can use \((\d+)\) (\S+):

\( - literal (
(\d+) - 1st capturing group, composed of digits
\) - literal )
  - literal space
(\S+) - 2nd capturing group, composed of non-spaces

Then extract the 1st and 2nd capturing groups.


const data = [
  "(2) 2020-09-15", "(3) 2020-09-16"
];
const numbers = [];
const dates = [];
for (const item of data) {
  const [, number, date] = item.match(/\((\d+)\) (\S+)/);
  numbers.push(number);
  dates.push(date);
}
console.log(numbers.join('\n'));
console.log(dates.join('\n'));