-1

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

migarcia
  • 1
  • 4
  • As someone posted on Twitter recently, `flatMap` is a rather misleading method name, it is actually `mapFlat`. See the answers… – HynekS Mar 10 '23 at 07: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 – migarcia Mar 13 '23 at 11:24
  • @migarcia answer from Unmitigated shows you how to flatten a (deeply) nested array correctly. There's nothing to be said here. – Mike Szyndel Mar 19 '23 at 07: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"` – Mike Szyndel Mar 19 '23 at 07: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. – migarcia Mar 20 '23 at 09:46
  • But it is the same, given the zero support provided, it would be better to look for other sources. – migarcia Mar 20 '23 at 09:48
  • No hay forma de conseguir que alguien COMPRENDA que NO ESTA DUPLICADO?. Esta bien, me voy de stackoverflow ya que asi lo quereis!!!! – migarcia Mar 21 '23 at 16:05

2 Answers2

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);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 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' ] – migarcia Mar 10 '23 at 08:56
0

.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"));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 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' ] – migarcia Mar 10 '23 at 08:54