[英]javascript: split string by space and make list using map and filter
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.因为这个字符串在一个字符串列表中,我不完全理解如何使用 map 和 filter 来完成这个。 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'] ]
simply this way...就这样……
const cars = [ [ 'Ford', 'blue'], [ 'Mercedez', 'green'], [ 'Ford', 'red blue'], [ 'Mercedez', 'black green'], [ 'Ford', 'red blue purple'], [ 'Mercedez', 'black green orange'] ] let bob = cars.map(e=> [e[0], e[1].split(' ')] ) console.log (bob)
You're mapping an array of arrays, so your argument inside of the map is an array itself.您正在映射一个数组数组,因此您在映射中的参数是一个数组本身。
So this should work:所以这应该有效:
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 (car) {
const colorString = car[1];
const colors = colorString.split(" ");
return [car[0], colors];
});
Or with more modern syntax ( still ES6 so it should work if you're using const and let) :或者使用更现代的语法(仍然是 ES6,所以如果你使用 const 和 let 应该可以工作):
let results = cars.map(([brand, colorString]) =>
[brand, colorString.split(" ")]);
const cars = [ ['Ford', 'blue'], ['Mercedez', 'green'], ['Ford', 'red blue'], ['Mercedez', 'black green'], ['Ford', 'red blue purple'], ['Mercedez', 'black green orange'] ] const result = cars.reduce((acc, x) => { const firstElement = x[0]; let rest = x[1].split(' ') return [...acc, [firstElement, rest]] }, []) console.log(result)
You can restructure the car and colors this way:您可以通过以下方式重组汽车和颜色:
const cars = [ [ 'Ford', 'blue'], [ 'Mercedez', 'green'], [ 'Ford', 'red blue'], [ 'Mercedez', 'black green'], [ 'Ford', 'red blue purple'], [ 'Mercedez', 'black green orange'] ] let results = cars.map(car => { const model = car[0]; const colors = car[1].split(' '); return [model, colors]; }); console.log(results); // Or in one line. results = cars.map(car => [car[0], car[0].split('')]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.