I'm using Vue with Pinia store installed.
export default {
setup() {
let rows: Row[] = store.history.rows;
}
}
This works fine, but later I want to overwrite the array and filter it:
const filterArray = () => {
rows=store.history.rows;
for (let index = 0; index < rows.length; index++){
if (rows[index].department !== departmentModel.value) {
rows.splice(index, 1);
}
}
};
Somehow the filterArray method removes the objects from the store.history.rows array as well, so both of the arrays will be empty soon.
What I want to archive is every time the filterArray is called, the rows array will be overwritten by the store.history.rows array (which contains all of the objects) and then the rows array will be filtered based on the if statement.
What am I doing wrong?
rows=store.history.rowsis not a shallow copy but rather reference to the same array.rows = [...store.history.rows]is a shallow copy, which is fine as long as the array items themselves are not getting mutated.