Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a unique value in an array using jquery

Tags:

arrays

jquery

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..

like image 923
yamini meenakshi Avatar asked Mar 19 '26 16:03

yamini meenakshi


1 Answers

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);

});
like image 144
void Avatar answered Mar 22 '26 04:03

void



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!