I am trying to split a string to a list of strings. Because this string is inside a list of strings, I don't fully understand how to use map and filter to accomplish this. Here is what I am working with:
const cars = [
[ 'Ford', 'blue'],
[ 'Mercedez', 'green'],
[ 'Ford', 'red blue'],
[ 'Mercedez', 'black green'],
[ 'Ford', 'red blue purple'],
[ 'Mercedez', 'black green orange']
]
let results = cars.map(function(carString){
let result = [];
// Regular expression to split only on the first white space
let strings = carString[1].split(/(?<=^\S+)\s/);
let arrOfStrings = [];
result.push(carString[0])
const isUndefined = string => typeOf(string) === undefined;
// I am not sure how to use map on a condition so here is what I tried
for (let i = 0; i < carString.length; i++) {
if (isUndefined == false) {
arrOfStrings.push(strings[i]);
}
}
result.push(arrOfStrings)
return result;
});
console.log(results);
And this is what I am trying to achieve:
[ 'Ford', ['blue'] ],
[ 'Mercedez', ['green'] ],
[ 'Ford', ['red', 'blue'] ],
[ 'Mercedez', ['black', 'green'] ],
[ 'Ford', ['red', 'blue', 'purple'] ],
[ 'Mercedez', ['black', 'green', 'orange'] ]