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

118
Views
Comparando entre 2 matrices anidadas diferentes en JavaScript

He estado en esto por un tiempo, pero no he podido llegar al resultado deseado. Me gustaría comparar elementos en dos matrices en función de sus índices. Algo como esto:

 const first = 0; const second = 1; const result = []; // should equal (false, true, false) const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]]; for (let i = 0; i < final.length; i++) { for (let j = 0; j < final[i].length; j++){ //Comparison between ((2 and 3) && (1 and 1)) with a single result. //Should also compare between ((4 and 2) && (3 and 0)) and many other nested arrays //This logic is not the right one as it can't do the comparison as intended. if (final[i][j][first] > final[i][j][second]) { result.push(true); } else result.push(false); }

Espero que esto se entienda, suficiente. Hay un problema muy similar a este. No sé si es correcto publicarlo aquí o abrir otro para hacer una pregunta, pero ambos son similares.

Muchas gracias.

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

0

Puedes probarlo :

 const first = 0; const second = 1; const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]]; const result = final.map(([e1, e2]) => (e1[first] > e1[second] && e2[first] > e2[second])) console.log(result) // with non arrow function function compareArr(arr, first, second) { return arr.map(function (ele) { return ele[0][first] > ele[0][second] && ele[1][first] > ele[1][second] }) } console.log(compareArr(final, first, second)) // with non map function : function compareArr1(arr, first, second) { let result1 = [] for(let i = 0; i < arr.length; i++) { result1[i] = true for(let j = 0; j < arr[i].length; j++) { if(!(arr[i][j][first] > arr[i][j][second])) result1[i] = false } } return result1 } console.log(compareArr1(final, first, second))

ACTUALIZACIÓN : editar con sugerencia de @ sí señor

Actualización : acerca de ([e1,e2]) :

normal: Array.map((ele) => .....) con estructura de ele es [a, b]

Puede usar lo mismo que: Array.map(([a, b]) => .....)

Documento de ello : Asignación de desestructuración

about 4 years ago · Juan Pablo Isaza Report

0

Xupitan usó programación funcional, que es el mejor enfoque en mi opinión, pero traté de extender su código.

lo que antes le faltaba era que no estaba realizando un seguimiento de la primera matriz, estaba comparando cada matriz anidada, este enfoque también es válido, pero luego debe comparar cada resultado, por ejemplo, resultado [i] && resultado [i + 1] .

 const first = 0; const second = 1; const result = []; // should equal (true, true, false) const final = [[[2,3],[1,1]],[[4,2],[3,0]],[[0,3],[1,1]]]; let k=0 for (let i = 0; i < final.length; i++) { for (let j = 0; j < final[i].length; j++){ //Comparison between ((2 and 3) && (1 and 1)) with a single result. //Should also compare between ((4 and 2) && (3 and 0)) and many other nested arrays //This logic is not the right one as it can't do the comparison as intended. if (final[i][j][first] > final[i][j][second]) { if(k > 0){ result[i] = true } result[i] = true }else{ //first false exit result[i] = false; break; } k++ } k=0 } console.log(result)

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!