I am currently able to display my meals output, however, there are duplicates that show up. To compensate, I tried to use the logic seen here: Removing duplicate array values and then storing them [react]. However, now my list disappears. If I can get some help, I would much appreciate it as I have been trying to debug this for quite a bit. Thank you!
Relevant code:
const [meals, setMeals] = React.useState([]);
useEffect(() => {
getMeals();
}, []);
function getMeals() {
console.log(route.params.DiningID);
fetch(`a https url`, {
method: 'GET',
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
})
.then(
function(response) {
if (response.status === 200 || response.status === 201) {
// Successful GET
// Set Fields to correct values
response.json().then(function(data) {
setMeals(data.map(menuItem => ({ label: menuItem.menu_item.item_name, value: menuItem.menu_item.menu_item_id })));
// having trouble with the unique output
setMeals(meals
.map(e => e['value'])
// store the keys of the unique objects
.map((e, i, final) => final.indexOf(e) === i && i)
// eliminate the dead keys & store unique objects
.filter(e => meals[e]).map(e => meals[e]));
});
} else {
console.log('Getting Dining Menu Items like there was a problem. Status Code: ' +
response.status);
}
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
}