0

I was writing about version news and I came across something that I think is a bug in javascript. The code:

const arrValor = [1,2,3,4,5,,6,[7,8,9,[10,11,[111,222]],12],13,14];
//console.log("Original :",arrValor);
console.log("mas s  :",arrValor.flatMap(val=>val+"s"));

the result:

mas s  : [
  '1s',
  '2s',
  '3s',
  '4s',
  '5s',
  '6s',
  '7,8,9,10,11,111,222,12s',
  '13s',
  '14s'
]

The detail is in the 12, since it is at the same level as the 7,8,9 and to those it does not add the s... Why to the 12?

Test with Node v18.13.0 en windows 10Pro 21H2

I expected the layered application to be homogeneous, and I have tried mixed numeric and alphabetic arrays, but it always behaves the same Attention editors: My question is not how to flatten an array, but a strange behavior in the function that makes me think that it does not work well, and that is what I am getting

7
  • 1
    As someone posted on Twitter recently, flatMap is a rather misleading method name, it is actually mapFlat. See the answers… Commented Mar 10, 2023 at 7:47
  • Attention editors: My question is not how to flatten an array, but a strange behavior in the function that makes me think that it does not work well, and that is what I am getting Commented Mar 13, 2023 at 11:24
  • @migarcia answer from Unmitigated shows you how to flatten a (deeply) nested array correctly. There's nothing to be said here. Commented Mar 19, 2023 at 7:18
  • To answer your other question, it's just a quirk of Javascript. See what the result of this expression is [1,2,3]+"s" Commented Mar 19, 2023 at 7:23
  • In principle, the only thing that is duplicated between my question and the one pointed out to me is the word flatMap. I'm not asking how to flatten the array, but pointing out a possible error in Javascript, and that's why I'm asking where to point out the error. I'm sorry for the translations, I'm using google translate from Spanish. Commented Mar 20, 2023 at 9:46

2 Answers 2

1

.flatMap will call the callback on every element of the array provided, and then flatten the result. It's useful if you return an array inside the callback - but that's not the logic you're looking for here. If you want to add an s to every element, including every nested element, and have a flat arra at the end - then you need to flatten then map, rather than map then flatten.

const arrValor = [1, 2, 3, 4, 5, , 6, [7, 8, 9, [10, 11, [111, 222]], 12], 13, 14];
console.log(arrValor.flat(Infinity).map(val => val + "s"));

Sign up to request clarification or add additional context in comments.

1 Comment

I think it is not normal that I do not add the s to 7,8 and 9, and to 12 yes !!!!! mas s : [ '1s', '2s', '3s', '4s', '5s', '6s', '7,8,9,10,11,111,222,12s', '13s', '14s' ]
1

flatMap (like calling flat() with no arguments) will only flatten one level. To handle arbitrarily nested arrays, you can either pass a recursive callback function to flatMap, or use .flat(Infinity) before applying map.

const arrValor = [1,2,3,4,5,,6,[7,8,9,[10,11,[111,222]],12],13,14];
let res = arrValor.flat(Infinity).map(val => val + 's');
console.log(res);

1 Comment

I may not have expressed myself clearly; I am using the Google translator, but... Why don't you add the s to 7, and to 12? mas s : [ '1s', '2s', '3s', '4s', '5s', '6s', '7,8,9,10,11,111,222,12s', '13s', '14s' ]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.