Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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: '' };
share|improve this question
up vote 2 down vote accepted

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.