javascript: split string by space and make list using map and filter

Viewed 875

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'] ]
4 Answers

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) :

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('')]);

Related