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

178
Views
¿Cómo dividir una matriz en matrices más pequeñas cada vez que aparece un valor específico JavaScript?

¿Cómo haría para dividir una matriz en la palabra 'dividir' y crear sub-matrices más pequeñas a partir de ellos?

Así es como se ve mi matriz en este momento (pero mucho más tiempo):

 const myArray = ['abc', 'xyz', 123, 'split', 'efg', 'hij', 456, 'split'];

Así es como me gustaría que se viera:

 const newArray =[['abc', 'xyz', 123], ['efg', 'hij', 456]];

Si ayuda, también tengo los índices de las palabras 'dividir' en una matriz como esta:

 const splitIndex = [3, 7];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede iterar splitIndex y dividir la matriz hasta este índice.

 const data = ['abc', 'xyz', 123, 'split', 'efg', 'hij', 456, 'split'], splitIndex = [3, 7], result = []; let i = 0; for (const j of splitIndex) { result.push(data.slice(i, j)); i = j + 1; } console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

const myArr = ['abc', 'xyz', 123, 'split', 'efg', 'hij', 456, 'split']; const foo = (arr, key) => { let temp = []; const result = []; arr.forEach(v => { if (v !== key) { temp.push(v); } else { result.push(temp); temp = []; } }) return result; } console.log(foo(myArr, 'split'));

producción:

 [ [ 'abc', 'xyz', 123 ], [ 'efg', 'hij', 456 ] ]
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!