split string in two on given index and return both parts

Try this

function split_at_index(value, index)
{
 return value.substring(0, index) + "," + value.substring(index);
}

console.log(split_at_index('3123124', 2));

ES6 1-liner

// :: splitAt = (number, any[] | string) => [any[] | string, any[] | string]
const splitAt = (index, xs) => [xs.slice(0, index), xs.slice(index)]

console.log(
  splitAt(1, 'foo'), // ["f", "oo"]
  splitAt(2, [1, 2, 3, 4]) // [[1, 2], [3, 4]]
)
  

Tags:

Javascript