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

149
Views
Problema de iteración de matriz bidimensional, se crea un elemento fantasma

Quiero crear matrices que contengan dos dimensiones de longitud <= 3. Esto debería llenarse dinámicamente con la siguiente función:

 let preppedArray = [] let currIndex = -1 let innerIndex = 0 for (let i = 0; i < props.children.length; i++) { if (i%3 === 0) { currIndex++ innerIndex = 0 preppedArray.push ([currIndex,"dummy"]) let currElement = props.children[i] preppedArray[currIndex][innerIndex] = currElement } else{ innerIndex++ preppedArray[currIndex][innerIndex] = props.children[i] } }

Curiosamente, la siguiente es la salida:

 Array(3) [ (3) […], (3) […], (2) […] ]​ 0: Array(3) [ {…}, {…}, {…} ]​​ 0: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ 1: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ 2: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ length: 3​​ <prototype>: Array []​ 1: Array(3) [ {…}, {…}, {…} ]​​ 0: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ 1: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ 2: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ length: 3​​ <prototype>: Array []​ 2: Array [ {…}, "dummy" ]​​ 0: Object { "$$typeof": Symbol("react.element"), type: Card(props) , key: null, … }​​ 1: "dummy"​​ length: 2​​ <prototype>: Array []​ length: 3​ <prototype>: Array []

Parece que el algoritmo crea un elemento ficticio "fantasma" al final. Sin embargo, no entiendo por qué o cómo solucionarlo. ¡Muchas gracias de antemano!

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

0

Entonces encontré una solución mucho más fácil usando slice:

 let preppedArray = [] let currIndex = -1 for (let i = 0; i < props.children.length; i++) { if (i%3 === 0) { currIndex++ preppedArray.push (props.children.slice(i,i+3)) } }

Esto crea una matriz bidimensional con la cantidad deseada de subelementos.

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!