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)