Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
How to get all possible 3 element combinations from an array of n elements in JavaScript?

Suppose I have the below array of integers in JavaScript:

[5,7,6,1,7,5,4,8,2,4]

And I want to create a function that extracts all possible 3 element combinations from it, like below:

[[5,7,6],[5,7,1],[5,7,7],etc]

What is the most performant and shortest way to do it?

Is there a better way to do it than for loops?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think the simplest and most performant way to do it is for loops like this

const data = [5,7,6,1,7,5,4,8,2,4]

const combinations = []

for(let i = 0; i < data.length -2; i++){
  for(let j = i + 1; j < data.length -1; j++){
    for(let k = j + 1; k < data.length; k++){
       combinations.push([data[i],data[j],data[k]])
    }
  }
}

console.log(combinations)

There are more elegant ways to do it but less performant

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda