I'm trying to add new values to an array without duplicates.
I'm using Map object to hold the previous array and trying to filter the new array
const [items, setItems] = useState([]);
const res = () => {
const map1 = new Map(
arr.map(obj => {
return [obj.id, obj];
})
);
setItems([map1, ...data.result.filter(x => x.id !== map1.forEach((val, key) => key))]);
};
How to accomplish this iteration successfully? Are there other ways with using Map object that would be better in this case?