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

101
Views
Divida la matriz en un fragmento y luego elimine el fragmento de la matriz original

Este es el tipo de datos que tengo:

 ["2", "Something1", "Something2", "4", "Something3", "Something4", "Something5", "Something6", "2", "Something7", "Something8"]

El número indica qué tan grande debe ser el trozo.

 const splitInput = (input: string[]) => { const chunks: string[][] = []; input.forEach((element) => { if (element.length === 1) { const chunk = input.splice(input.indexOf(element) + 1, parseInt(element)); console.log(chunk); chunks.push(chunk) } });

El código anterior funciona solo hasta que no hay los mismos números, ya que indexOf encuentra la primera aparición. ¿Cómo puedo calcular fromIndex de indexOf() correctamente o eliminar todos los elementos eliminados de la matriz (incluidos los números)?

Esta sería la salida esperada

 [ ["Something1", "Something2"], ["Something3","Something4","Something5","Something6"],["Something7", "Something8"] ]

Antes de que alguien pregunte, sí, esto es una parte (no todo, esto es solo un comienzo, tendré que hacer algo con los trozos) de mi tarea. He estado probando todo durante las últimas 2 horas y no pude resolverlo.

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

0

No sé cómo arreglar su enfoque, pero otro enfoque es iterar sobre los tamaños de los fragmentos y llenar otra matriz con los fragmentos:

 function splitInput(elements) { const result = []; for (let i = 0, end = elements.length; i < end; i += 1 + +elements[i]) { result.push(elements.slice(i + 1, i + 1 + +elements[i])); } return result; } const elements = ["2", "Something1", "Something2", "4", "Something3", "Something4", "Something5", "Something6", "2", "Something7", "Something8"]; console.log(splitInput(elements));

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!