I know there are similar questions related to this but none of them seems to solve the issue for me.
How do prevent the push method from overwriting the existing object in the array?
Pardon the variable x
by the way.
const investorValues = state.investorSelected;
const categoryValues = state.categoryCheckbox;
const listAsFavourite = {
listName: "",
listDescription: "",
listItems: {
categories: [],
investors: [],
},
saveAsOpen: state.saveAsOpen === false ? !state.saveAsOpen : state.saveAsOpen,
saveAsComing: state.save
> Blockquote
AsComing === false ? !state.saveAsComing : state.saveAsComing
};
if (investorValues !== null) {
listAsFavourite.listItems = {
...listAsFavourite.listItems,
investors: investorValues
}
}
if (categoryValues !== null) {
listAsFavourite.listItems = {
...listAsFavourite.listItems,
categories: categoryValues
}
}
const saveFavouriteList = () => {
const favouriteList = []
if (state.favouriteListName === "") {
alert.error("List must have a title")
} else {
const x = {
...listAsFavourite,
listName: state.favouriteListName,
listDescription: state.favouriteListDescription
}
favouriteList.push(x);
localStorage.setItem("FAVOURITE_lIST", JSON.stringify(favouriteList));
alert.success("List created");
removeFavouriteListModal();
}
}