0
let rowsArrT = [];
let currentRow;

rowsEle = ['row1', 'row2', 'row3', 'row4']

function setRows(rows) {
    rows.forEach((row, ind) => {
        rowsArrT.push({ row, id: ind, next: false, notes: 0 })
    });
    rowsArrT[0].next = true;
    localStorage.setItem('TestRows', JSON.stringify(rowsArr));

}

function getCurrentRow() {
    rowsArrT.map((row, ind) => {
        if (row.next === true) {
            console.log(row)
            currentRow = row;
            rowsArrT[ind].next = false;
            if (ind !== rowsArrT.length - 1) {
                rowsArrT[ind + 1].next = true;
            } else {
                rowsArrT[0].next = true;
            }
        }
    })
}

setRows(rowsEle)

window.addEventListener('click', () => {
    getCurrentRow()
    console.log('-----After generate-----')
    console.log(currentRow)
    console.log(rowsArrT)

})

when I loop through this array and change its properties depending on some conditions , all the array's element property getting change to the condition that I added!, but when I check again outside the loop, the current element got stuck into the fourth row, It should change with each event

I tried to loop through this loop items, and change its 'next' property in sequence, but I still get stuck in the row 4, and loop don't work as expected

kholy
  • 1
  • 1
  • Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). Try using your browser’s [debug capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). Let your debugger illustrate what happens inside `getCurrentRow`. You change `next` in an iteration, but in the next iteration, you check `next` where you just set it, and set it elsewhere. – Sebastian Simon Jan 24 '23 at 04:39
  • You probably want a different method, e.g. [`findIndex`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex), instead of [`map`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – Sebastian Simon Jan 24 '23 at 04:41

0 Answers0