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

156
Views
¿Cómo crear un método de búsqueda personalizado?

Estoy tratando de crear un método de búsqueda personalizado usando Array.prototype , lo intenté de muchas maneras pero no pude romper el bucle para la primera coincidencia:

 let arr = ["Bill", "Russel", "John", "Tom", "Eduard", "Alien", "Till"]; Array.prototype.myFind = function (callback) { for (let i = 0; i < this.length; i++) { callback(this[i], i, this); } }; arr.myFind((element, index, array) => { if (element[0] === "T") { console.log(`This is ${element} in index of ${index} in array ${array}`); } });

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

0

Bueno, ahora solo tiene un ciclo for, si desea encontrar cualquier elemento que satisfaga una condición, debe devolver un valor booleano de su devolución de llamada y devolver el elemento, para el cual el valor booleano devuelto es true . Si desea que también se devuelva el índice del elemento que encontró y la matriz inicial, puede devolver un objeto y desestructurarlo:

 let arr = ["Bill", "Russel", "John", "Tom", "Eduard", "Alien", "Till"]; Array.prototype.myFind = function (callback) { for (let i = 0; i < this.length; i++) { if ( true == callback(this[i], i, this)) { return {element:this[i],index:i,array:this}; } } }; let {element,index,array} = arr.myFind((element) => element[0] === "T"); console.log(`This is ${element} in index of ${index} in array ${array}`);

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!