Trimming an array of arrays (Javascript)

jgalec

I know how to use this method to trim every string in an array ( thanks to this article! )map()

let a = [' a ', ' b ', ' c '];

let b = a.map(element => {
  return element.trim();
});

console.log(b); // ['a', 'b', 'c']

But how can you trim an array of arrays?

for examplelet x = [[" a ", " b "], [" c ", " d "]]

thank you!

PS: I'm currently learning Javascript.

axtck

You can also map to a nested array.

let data = [[" a ", " b "], [" c ", " d "]]

const result = data.map((d) => d.map((y) => y.trim()));

console.log(result);

If you want to flatten the results, you can use:flatMap()

let data = [[" a ", " b "], [" c ", " d "]]

const result = data.flatMap((d) => d.map((y) => y.trim()));

console.log(result);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324200991&siteId=291194637