-1

I have an array look like these :

console.log(myArray);

myArray: [100000, 300000, 20000000, 3450000, 610000]

What I want to do is converting my array into like these:

myArray: [100.000, 300.000, 20.000.000, 3.450.000, 610.000]

So far, I've tried code:

for (let i = 0; i < myArray.length; i++) {
   const element = (myArray[i]/1000).toFixed(3);
}
console.log(element);

But it seems didn't work at all. Anyone can help me to solve these?

Thanks.

baimWonk
  • 761
  • 1
  • 5
  • 11
  • What's `3.450.000` supposed to be? 3 million something? 3 thousand something? 3 something? – Robby Cornelissen Oct 10 '19 at 09:06
  • yes, sir 3 million Imean @RobbyCornelissen – baimWonk Oct 10 '19 at 09:07
  • `element` is declared inside `for` loop and you are logging it outside. Use `myArray[i] = myArray[i]/1000...` inside the loop. Besides, it's not clear whether you want `.` as a thousands separator OR you are trying to divide the numbers by 1000 – adiga Oct 10 '19 at 09:07
  • Literally 3 seconds away from posting my answer and this gets closed... Use Intl.NumberFormat with your locale and a decimal style. – Martin Oct 10 '19 at 09:11

1 Answers1

0

You could use a Intl.NumberFormat with a locale that matches your required output (e.g. German/Germany):

const array = [100000, 300000, 20000000, 3450000, 610000];
const formatter = new Intl.NumberFormat('de-DE');
const formatted = array.map(formatter.format);

console.log(formatted);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156