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

113
Vistas
array elements does not match same assigned elements

I wrote a function where I am looping through an array(which contains a number of array elements itself).

e.g.

theMainAr:[ [ 5 ],[ 5, 3 ],[ 5, 3, 9 ],[ 5, 3, 9, 4 ],[ 3 ],[ 3, 9 ],[ 3, 9, 4 ],[ 9 ],[ 9, 4 ],[ 4 ] ]

The main array I got the above array from is :

weights: [5,3,9,4]

I want to get a combination from these above arrays like this:

sameLenAr:[[5,9],[5,4],[5,3,4],[3,4]]

But Everytime I get an output like this: [[5,4],[5,4],[5,3,4],[3,4]] The first element is not [5,9] Why?

let theMainAr = [ [ 5 ],[ 5, 3 ],[ 5, 3, 9 ],[ 5, 3, 9, 4 ],[ 3 ],[ 3, 9 ],[ 3, 9, 4 ],[ 9 ],[ 9, 4 ],[ 4 ] ]
let weights = [5,3,9,4], lenOfWeights = weights.length;
let sameLenAr = []
let inc = 0
for (let i = 1; i < theMainAr.length - 3; i++) {
  let curLen = theMainAr[i].length
  if (curLen > 1) {
    let curAr = theMainAr[i]
    let curArLen = curAr.length
    let indexOfEle = weights.indexOf(curAr[curLen - 1])
    for (let m = indexOfEle + 1; m < lenOfWeights; m++) {
      //   console.log(a)
      curAr[curArLen - 1] = weights[m]
      console.log(curAr)
      sameLenAr[inc] = curAr
      inc++
    }
  }
}

console.log(sameLenAr)

about 4 years ago · Juan Pablo Isaza
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