I have a groceryReducer where I add groceryItems to. The format of each object that is added to the groceryItems array is listed below. I'm trying to use the newSet array method, but it's still adding them and not filtering out the duplicates. What am I doing wrong?
groceryObject
{
title: "Chorizo Turkey Chili",
subtitle: "Soup/Stew"
}
groceryReducer
// Action Type: Add Grocery Item
case 'ADD_GROCERY_ITEM': {
return {
...state,
groceryItems: Array.from(new Set(state.groceryItems.concat(action.groceryObject))),
}
}
console.log
groceryItems: Array(4)
0: {title: "Chorizo Turkey Chili", subtitle: "Soup/Stew"}
1: {title: "Chorizo Turkey Chili", subtitle: "Soup/Stew"}
2: {title: "Chorizo Turkey Chili", subtitle: "Soup/Stew"}
3: {title: "Chorizo Turkey Chili", subtitle: "Soup/Stew"}
The
Setobject will keep unique references, it doesn't compare properties of an object.One way you could accomplish this is creating a set of titles and comparing those.