I want to push a unique value in an array and I'm using jquery
var otNotesTimeIntrArray = new Array();
$("#otNoteFluids").on('change',function() {
var otNotesTimeIntr = $("#otNotesTimeIntr").val();
otNotesTimeIntrArray.push(otNotesTimeIntr);
});
otNotesTimeIntr consists of Time intervals. Example: 10:15AM, 10:45AM...
If 10:15AM already exist, I don't want it to push into array..
Use can use .indexOf to check whether a value already exists in an array or not
var otNotesTimeIntrArray = new Array();
$("#otNoteFluids").on('change',function() {
var otNotesTimeIntr = $("#otNotesTimeIntr").val();
//Use .indexOf before pusing into array
if(otNotesTimeIntrArray.indexOf(otNotesTimeIntr)==-1)
otNotesTimeIntrArray.push(otNotesTimeIntr);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With