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

98
Views
¿La forma más eficiente de comparar múltiples matrices basadas en un elemento fijo y copiar la más grande?

Tengo cuatro matrices de longitud 18 ( k1 , k2 , k3 y k4 ). Quiero comparar el último elemento de cada matriz y copiar el contenido de la matriz con el elemento final más grande.

Actualmente, así es como lo estoy haciendo:

 if (this.k4[17]>=this.k1[17] && this.k4[17]>=this.k2[17] && this.k4[17]>=this.k3[17]) m = this.k4.filter(() => true); else if(this.k1[17]>=this.k4[17] && this.k1[17]>=this.k2[17] && this.k1[17]>=this.k3[17]) m = this.k1.filter(() => true); else if(this.k2[17]>=this.k4[17] && this.k2[17]>=this.k1[17] && this.k2[17]>=this.k3[17]) m = this.k2.filter(() => true); else m = this.k3.filter(() => true);

Pero esto no es escalable en caso de que agregue más matrices en el futuro.

Aquí hay otro método:

 var maxlast = [this.k1[17],this.k2[17],this.k3[17],this.k4[17]]; var max = maxlast.reduce(function(a,b){return Math.max(a, b)}, -Infinity); var pos = maxlast.indexOf(max)+1; m = eval("this.k"+pos+".filter(() => true);");

Escuché que eval() es un gran riesgo de seguridad, ¿existe un enfoque alternativo?

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

0

Puede usar ordenar y simplemente devolver el primer elemento de descending sorted list

 const arr = [this.k1, this.k2, this.k3, this.k4] // sort desc const arrayHasMaxLastElement = arr.sort((a, b) => b[b.length - 1] - a[a.length - 1])[0]
about 4 years ago · Juan Pablo Isaza Report

0

Otra forma de obtener el artículo máximo con reduce

 const arr = [[1,2,3], [3,4,5], [4,5,6], [7,8,9]]; const result = arr.reduce((max, item) => max.at(-1) > item.at(-1) ? max : item); console.log(...result);
 .as-console-wrapper{min-height: 100%!important; top: 0}

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!