I have a little form in my HTML that takes data for a reservation. The correspondent submit button is associated with a function (saveReserve). The idea is that the function could take the data and push it into an array, but also detects when a similar reservation has already been made, and so if it's the case, left the new one out (don't push it into the array with the one that is similar).
The issue is that all reservations are push even if they are the same, ignoring the IF element:
let allReservation = [];
var NR = 0
const saveReserve = () => {
var date= $("#date").val(); //These are the form inputs//
var hour= $("#hour").val();
var gameRes = $("#reserveG").val();
NR++
var reservation = ({date, hour, gameRes})
let exist = allReservation.find(e=> e.date == reservation.date && e.hour == reservation.hour && e.gameRes == reservation.gameRes);
if (exist == undefined) {
allReservation.push( NR + ": " + $("#nameR").val() + Object.values(reservation) ),
alert ("All good") )
}else{
alert ("Invalid, try again")
}
//nameR is the name input in the form//
console.log (allReservation)
$("input").val('')
}
The only way the .find works is if I add this at the end of the function:
allReservation.push (reservation)
Then it recognise the repeat elements and alerts them, but also push them in.
Aucun commentaire:
Enregistrer un commentaire