0

I am trying to remove items from an array. But i have to do it from each item from it. For that, I used a for loop, and ,in the begging of the loop, I try to renovate the array values by using the const seq.

But the sequence array keeps decreasing it's length as the for loop runs. I am trying to remove one item, do some stuff and put it back to the array so I can do the same thing with the next item.

const q = sequence;
for(let i= sequence.length-1; i>=0 ; i--){
        sequence = q;
        sequence.splice(i,1,);
        console.log(sequence);   
           //do some other stuff here

}

output ex:

Input:sequence: [1, 3, 2]

Console Output:[ 1, 3 ]
               [ 1 ]
               []

what i expected: [1, 3]
                 [1, 2]
                 [3,2]
  • `sequence = q;` does not copy the array, it just stores the reference to the array in a second variable – Bergi Mar 10 '22 at 20:36
  • as i know you want all possible subarray having length one lesser than existing array? – Nilesh Mishra Mar 10 '22 at 20:37
  • ```var subsetArray = (function() { return { getResult: getResult } function getResult(array, n) { function isBigEnough(value) { return value.length === n; } var ps = [ [] ]; for (var i = 0; i < array.length; i++) { for (var j = 0, len = ps.length; j < len; j++) { ps.push(ps[j].concat(array[i])); } } return ps.filter(isBigEnough); } })(); var arr = [1, 2, 3]; console.log(subsetArray.getResult(arr,2));``` – Nilesh Mishra Mar 10 '22 at 21:04

0 Answers0