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

74
Views
4to argumento en reducir

Aquí está la función:

 function chunk(array: number[], size: number): number[][] { return array.reduce((chunks, curr, _, arr) => { console.log(arr.length); // -> 10 which is correct // let len = arr.length; // -> Cannot read properties of undefined (reading 'length') let len = chunks.length; // this works if (len === 0 || chunks[len - 1].length === size) chunks.push([curr]); else chunks[len - 1].push(curr); return chunks; }, []); } console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)); // ->[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10 ] ]

El cuarto argumento para reduce es la matriz sobre la que estamos iterando. Puedo registrarlo y obtengo el resultado correcto (10) ver arriba. Pero cuando trato de usarlo y asignarlo a una variable, aparece un error (ver arriba). ¿Podría alguien arrojar algo de luz?

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

0

Desde la página de Mozilla , el cuarto parámetro es la matriz que se está reduciendo. Debe acceder a la variable de array que ya está declarada, pero el cuarto parámetro funciona.

Por ejemplo:

 array.reduce((_, __, ___, arr) => { console.log(arr.length == array.length) // true, this is the source array });

La razón por la que recibe el error no se debe a la propiedad arr.length , sino a la forma en que accede a los fragmentos.

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!