Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
How to insert into an array at a specific index?

I am trying to add a colon in between two numbers I receive from a 4 digit time. However I am receiving an empty string.

const num = 1234
num.toString().split('').splice(-2, 0, ':').join('')
console.log(num)

expected output:

'12:34'
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

splice operates on the array in place returning an array of deleted elements, or an empty array if none have been deleted. So while you can chain the method in those instances it won't work for your example.

const num = 1234;
const arr = num.toString().split('')
arr.splice(2, 0, ':');
const str = arr.join('');
console.log(str)

about 4 years ago · Santiago Trujillo Report

0

let num = 1234
num = num.toString().split('')
num.splice(-2, 0, ':')
num = num.join('')
console.log(num)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!