Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda