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

111
Views
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
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!