I am working on a requirement where i have to dynamically generate rows and columns and retain the values entered in each cell. I am trying to iterate over an complex array having array inside each element. The JSON structure looks like this:-
[
{
"rowNumber": 1,
"cellValues": [
{
"col": 1,
"value": "",
"uniqueid": "12"
},
{
"col": 2,
"value": "",
"uniqueid": "22"
}
]
},
{
"rowNumber": 2,
"cellValues": [
{
"col": 1,
"value": "",
"uniqueid": "12"
},
{
"col": 2,
"value": "",
"uniqueid": "22"
}
]
}
]
How I am trying to iterate?
I am using forEach to iterate and assign the values to uniqueId.
@track tableCellArray = [
{
col: 1,
value: ''
}
];
@track tableRowAndColumnArray = [
{
rowNumber: 1,
cellValues: this.tableCellArray
}
];
this.tableRowAndColumnArray.forEach(element => {
element.cellValues.forEach(cell => {
cell["uniqueid"] = JSON.stringify(cell.col)+JSON.stringify(element.rowNumber);
});
});
What problem i am facing?
The above forEach loop assign the same values for both rows. It should add different value for different rows.
To make you understand the issue, have a look on below pic:-
I do not understand, why its copying the same value under each rows?
