I`m new in React, I have array with server data (simplified)
let recipes = [
{"recipe": {label: "Chicken Vesuvio"},"bookmarked":false,"bought":false},
{"recipe": {label: "Chicken Paprikash"},"bookmarked":false,"bought":false},
{"recipe": {label: "Baked Chicken"},"bookmarked":false,"bought":false},
{"recipe": {label: "Chicken Liver Pâté"},"bookmarked":false,"bought":false}
]
And another one from localStorage
let localRecipes = [
{"recipe": {label: "Chicken Vesuvio"},"bookmarked":true,"bought":false},
{"recipe": {label: "Chicken Paprikash"},"bookmarked":true,"bought":false},
]
I need to compare if obj in array one equal to obj in array two, change value bookmarked in first array to true and return a copy of the array. I don’t have any id, so I compare using label. Here is my code it works, but it doesn`t return copy, it mutates an original one.
useEffect(() => {
if(recipes && recipes.length) {
for (let i = 0; i < localRecipes.length; i++) {
if(recipes[i].recipe.label == localRecipes[i].recipe.label) {
recipes[i].bookmarked = true
}
}
}
}, [isFetching])