You can use Array.concat(), since concat accepts both arrays and non arrays:
const names = (v) => [].concat(v).map(name => name.toUpperCase())
console.log(names(['Bart', 'Lisa'])) // [ 'BART', 'LISA' ]
console.log(names('Homer')) // ['HOMER']
Answer from Ori Drori on Stack OverflowYou can use Array.concat(), since concat accepts both arrays and non arrays:
const names = (v) => [].concat(v).map(name => name.toUpperCase())
console.log(names(['Bart', 'Lisa'])) // [ 'BART', 'LISA' ]
console.log(names('Homer')) // ['HOMER']
Short solution:
[names].flat()
If names is an array, it will be left as-is. Anything else will be converted to an array with one element.


This works because .flat() only flattens one level by default.
If names is not an array, [names] makes it an array with one element, and .flat() does nothing more because the array has no child arrays.
If names is an array, [names] makes it an array with one child array, and .flat() brings the child array back up to be a parent array.
Alternative
This is more self-explanitory:
names instanceof Array ? names : [names]
This uses a simple ternary statement to do nothing to it if it is an array already or make it an array if it is not already.
Use the .split() method. When specifying an empty string as the separator, the split() method will return an array with one element per character.
entry = prompt("Enter your name")
entryArray = entry.split("");
ES6 :
const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]
You could use the following string splitting approach:
var data = '[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]';
data = data.substring(2, data.length - 2);
var array = data.match(/\[.*?\]/g);
var output = [];
for (var i=0; i < array.length; ++i) {
output.push(array[i].substring(1, array[i].length - 1).split(/\s*,\s*/));
}
console.log(output);
It's not perfect but should get the jobs done:
const data = '[[Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16], [Bday Party , 2023-05-16]]';
const json = JSON.parse(data.replace(/\s*([^[\],]+?)\s*(?=[,\]])/g, '"$1"'));
console.log(json);
How the regex works:
\s*([^[\],]+?)\s*(?=[,\]])
Here you can see how it works and play around a little bit
| syntax | explain |
|---|---|
\s* |
any white spaces, this is for getting rid of the leading spaces from each entry |
( |
group one start, this is for capturing each entry, such as Bday Party |
[^[\],]+? |
1 or more characters that does not contain [, ] and ,. +? means 1 or more but non-greedy, this is for avoiding capturing trailing spaces (we can't rule out white spaces inside of the character set, since some of the entries contains white spaces in the middle) |
) |
group one end, so far we have captured the entry we need in group one, such as Bday Party |
\s* |
any trailing whitespaces after an entry, we don't need that in our capturing group |
(?=[,\]]) |
a lookahead, everything we have matched must be followed by a ,(list separator) or ](end of an array) |