我正在尝试将一个字符串拆分为字符串列表。由于这个字符串在一个字符串列表中,我不完全理解如何使用map和filter来完成这个任务。以下是我的工作内容:
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);
这就是我想要实现的内容:
[ 'Ford', ['blue'] ],
[ 'Mercedez', ['green'] ],
[ 'Ford', ['red', 'blue'] ],
[ 'Mercedez', ['black', 'green'] ],
[ 'Ford', ['red', 'blue', 'purple'] ],
[ 'Mercedez', ['black', 'green', 'orange'] ]