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
Compare dos matrices y, si coincide, pase al objeto

La función ha probado cada elemento de la primera matriz utilizando la devolución de llamada para ver si la salida coincide con el elemento correspondiente (por índice) de la segunda matriz. Si hay una coincidencia, el elemento de la primera matriz se convierte en una clave en un objeto y el elemento de la segunda matriz se convierte en el valor correspondiente.

 function objOfMatches(arr1, arr2, callback){ let obj ={}; let newArr1 = arr1.toString(); newArr1 = callback(newArr1); newArr1 = newArr1.split(","); arr1.forEach(el=>{ if((callback(newArr1) === arr2[el])) obj[arr1[el]] = arr2[el]; }); return obj; }

El problema que tengo es que cuando llamo a una función como esta:

 const arr1 = ['hi', 'howdy', 'bye', 'later', 'hello']; const arr2 = ['HI', 'Howdy', 'BYE', 'later', 'HELLO']; function uppercaser(str) { return str.toUpperCase(); }; console.log(objOfMatches(arr1, arr2, uppercaser));

Da un error (Error de tipo en la línea 18: str.toUpperCase no es una función).

Sin embargo, funciona perfectamente bien si se usa con un bucle FOR en lugar de .forEach como este:

 for(let i=0; i<arr1.length; i++){ if(callback(arr1[i]) === arr2[i]){ obj[arr1[i]] = arr2[i]; } }

¿Puede alguien explicarme en un lenguaje de nivel principiante de JS por qué no funciona?

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

0

En resumen, newArr1 se ha convertido en una matriz cuando llamó a .toUpperCase() , de ahí el TypeError. Echemos un vistazo más de cerca a su función:

 function objOfMatches(arr1, arr2, callback) { let obj = {}; let newArr1 = arr1.toString(); // string newArr1 = callback(newArr1); // string newArr1 = newArr1.split(','); // array arr1.forEach((el) => { if (callback(newArr1) === arr2[el]) { // Error, newArr1 is an array and doesn't have the method .toUpperCase() obj[arr1[el]] = arr2[el]; } }); return obj; }

Sin mencionar que la lógica de su función es incorrecta. Según su descripción, debería ser más como esto:

 function objOfMatches(arr1, arr2, callback) { let obj = {}; arr1.forEach((el, index) => { if (callback(el) === arr2[index]) { obj[el] = arr2[index]; // Eg { 'hi': 'HI' } } }); return obj; }

La versión del bucle for es realmente correcta, por lo que funciona:

 function objOfMatches(arr1, arr2, callback) { let obj = {}; for(let i=0; i<arr1.length; i++){ if(callback(arr1[i]) === arr2[i]){ // Correct, arr1 is an array of string so each string will have .toUpperCase() obj[arr1[i]] = arr2[i]; } } return obj; }
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!