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

122
Views
¿Cómo recorrer la matriz con FindIndex desde su extremo?

Necesito obtener errorIdx = 3 , pero obtengo 0. ¿Cómo puedo recorrer la matriz desde su extremo?

 const scrollPosition = 5007 const errorsHeight = [947, 2498, 3495, 4805, 5755] errorIdx = errorsHeight.findIndex((itemHeight: number) => itemHeight < scrollPosition) console.log(errorIdx) // 0
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Una implementación alternativa de findLastIndex donde la función de devolución de llamada reconoce un contexto/objetivo thisArg y también se invocará con sus tres parámetros que son [element, index, array] ; siguiendo así el estándar de findIndex ...

 function findLastIndex(arr, test, target) { if (!arr && ((arr ?? true) === true)) { throw new TypeError('findLastIndex called on null or undefined'); }; if (typeof test !== 'function') { throw new TypeError(`${ test } is not a function`); }; if (!Array.isArray(arr)) { arr = Array.from(arr); } target = target ?? null; let isContinue = true; let idx = arr.length; // assures -1 as return value for nothing found. while ((idx >= 0) && isContinue) { // be aware of sparse array slots ... and ... // be a guard for the negative index default. if (arr.hasOwnProperty(--idx)) { isContinue = !test.call(target, arr[idx], idx, arr); } } return idx; } const errorsHeight = [947, 2498, 3495, 4805, 5755]; const scrollPosition = 5007; console.log( findLastIndex(errorsHeight, (height/*, idx, arr*/) => height < scrollPosition ) ); console.log( findLastIndex(errorsHeight, height => height < 5) );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Al revertir la matriz o invertirla, se agrega un O(n) adicional a su solución.

El enfoque simple es simplemente hacer una función auxiliar que encuentre el índice comenzando desde el final de la matriz:

 function findLastIndex(arr, comparator){ for(let i = arr.length - 1; i > 0; i--){ const valid = comparator(arr[i], i); if(valid){ return i; } } return -1; } console.log(findLastIndex(errorsHeight, (itemHeight: number) => itemHeight < scrollPosition))
about 4 years ago · Juan Pablo Isaza Report

0

Mejor hacerlo con un for simple, como este:

 const scrollPosition = 5007; const errorsHeight = [947, 2498, 3495, 4805, 5755]; let errorIdx = -1; for (let i = 0; i < errorsHeight.length; i++) if((errorIdx === -1 && errorsHeight[i] < scrollPosition) || (errorIdx >= 0 && errorsHeight[i] < scrollPosition && errorsHeight[i] > errorsHeight[errorIdx])) errorIdx = i; console.log(errorIdx) //3
;D

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!