简体   繁体   中英

How to check if item already exists in array in vue.js?

I have this array where i add values on click, but i want to check if value is already in array, if it is do nothing. I tried with indexOf but i get same result every time

this.fields.push(this.field);
      this.field = { value: '' };

Are you determining if it's in the array by the value property? If so you can use Array.some() .

var exists = this.fields.some(function(field) {
  return field.value === this.field.value
});

if (!exists) {
  this.fields.push(this.field);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM