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

250
Views
javascript [1,2,3,4,5,6,7] se convierte en 123, 234, 345, 456, etc.

Me gustaría pedir consejo sobre cómo dejar que esta matriz [1,2,3,4, 5, 6, 7] se convierta en 123, 234, 345, 456, 567.

esta inicial es un número 1234567, los dividí

 const num = 1234567; const result = String(num) .split("") .map((n, i, src) => (src.length !== i + 1 ? `${n}${src[i + 1]}` : null)) .filter(Boolean) .map(Number); console.log(result)

el resultado fue 12, 23, 34, 45 etc.

¿Cómo hacer que se convierta en 123, 234, 345, 456?

gracias por ayudar

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La solución más simple será recorrer la matriz y recorrer hasta la length - 2 como:

 const num = 1234567; const strNum = String(num); const result = []; for (let i = 0; i < strNum.length - 2; ++i) { result.push(strNum.slice(i, i + 3)); } console.log(result);

You can even create a generic function and get the result string of desired length

 const num = 1234567; const strNum = String(num); function getPartialString(str, len) { const result = []; for (let i = 0; i < strNum.length - (len - 1); ++i) { result.push(strNum.slice(i, i + len)); } return result; } console.log(getPartialString(strNum, 3)); console.log(getPartialString(strNum, 4)); console.log(getPartialString(strNum, 5));
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza 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!