-1

I have this array of strings.

const numbersArray = ['1000','10000','100000']

My goal is to split each one of them on specific index for example: output of 1000 should be 1,000 and etc...

Here is what i have right now:

  const splitArrayHandler = (arr) =>{
  for (let i = 0; i < arr.length; i++) {
    let indexOfSymbol = Math.round(arr[i].length / 3)
    return splitAtIndex(arr[i],indexOfSymbol)
  }
}

const splitAtIndex = (value,index) => {
  return value.substring(0,index) + ',' + value.substring(index)
}

splitArrayHandler(numbersArray)

The first function splitArrayHandler loops through my array,finds specific index of the symbol in the string and then function splitAtIndex does the rest of the hard work.

The problem is only first element of the string is passing to the splitAtIndexfunction and I dont understand why. any suggestions please?

const numbersArray = ['1000','10000','100000']

const splitArrayHandler = (arr) =>{
  for (let i = 0; i < arr.length; i++) {
    let indexOfSymbol = Math.round(arr[i].length / 3)
    return splitAtIndex(arr[i],indexOfSymbol)
  }
}

const splitAtIndex = (value,index) => {
  return value.substring(0,index) + ',' + value.substring(index)
}

splitArrayHandler(numbersArray)
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
RoffyBC
  • 189
  • 1
  • 8
  • https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – knosmos Apr 11 '21 at 14:02
  • Don't use regex/string parsing for formatting numbers to a certain locale. Javascript has a built-in object for the job. – connexo Apr 11 '21 at 14:23
  • Why are you trying to implement what is already built-in? All suggested methods except using `Intl.NumberFormat` will fail e.g. for `9999.99`. – connexo Apr 11 '21 at 18:45

3 Answers3

3

Use Intl.NumberFormat for the job. No need for string parsing / manipulating:

const numbersArray = ['1000', '10000', '100000', '654654686156', '1000.66', '10e14', '0xFFFF'];
const format = new Intl.NumberFormat('en-US').format;

const formattedNumbers = numbersArray.map(Number).map(format);

console.log(formattedNumbers);
connexo
  • 53,704
  • 14
  • 91
  • 128
1

You might use regular expression and map function (though there is no real difference between map and hard coded loop)

const numbersArray = ['1000','10000','100000']
function addComa(x) {
    return x.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}
const resolved = numbersArray.map(addComma)
console.log(resolved) // ['1,000','10,000','100,000']
Hagai Harari
  • 2,767
  • 3
  • 11
  • 23
  • I tried that way too but it still only returns single element from list. I need to return all of them – RoffyBC Apr 11 '21 at 14:05
  • How well does this work for decimals like `9999.99`? – connexo Apr 11 '21 at 18:46
  • As long there is no 4 digits or more after the `.` the code will work as expected. Otherwise add checker in the regex for not being after the `.` or `split` the string at `.`, manipulate `arr[0]`, and `join('.')` – Hagai Harari Apr 14 '21 at 05:53
1

You are breaking the loop by returning the splitAtIndex function. Create another array and push the results to it.

const splitArrayHandler = (arr) =>{
  let arr2 = []
  for (let i = 0; i < arr.length; i++) {
    let indexOfSymbol = Math.round(arr[i].length / 3)
    arr2.push(splitAtIndex(arr[i],indexOfSymbol))
  }
  return arr2
}
  • 1
    Thank you very much! I will accept your answer in 4 minutes – RoffyBC Apr 11 '21 at 14:09
  • 2
    you are right about `return` un correct usage, though he can overwrite same array and not push to new one `arr[i] = splitAtIndex(arr[i],indexOfSymbol` – Hagai Harari Apr 11 '21 at 14:10
  • Don't use regex/string parsing for formatting numbers to a certain locale. Javascript has a built-in object for the job. – connexo Apr 11 '21 at 18:39