-1

I've looked all over for an example of this but can't figure it out.

$('.ac-items').on(flexClick, function () {
    var $this = $(this);
    var inputSelection = $this.parent().parent().find('input').val();
    var currentField = $this.parent().parent().find('input');
    var crewInputs = $('input[id^="crew-list-"]');
    var crewChoices = new Array ();
    for (i=0; i<crewInputs.length; i++) {
        crewChoices.push(crewInputs[i].value);
    }
    for (a = 0; a < crewChoices.length; a++) {
        if (inputSelection == crewChoices[a]) {
            alert('try again');
        }

    }

 });
NewtonEntropy
  • 87
  • 1
  • 3
  • 12

2 Answers2

0

Test if your value is already in the array. If not, then push it.

    var crewChoices = new Array ();
    for (i=0; i<crewInputs.length; i++) {
        if($.inArray( crewInputs[i].value, crewChoices ) != -1) {
                crewChoices.push(crewInputs[i].value);
        }
    }
Daniel
  • 4,816
  • 3
  • 27
  • 31
0

Are you looking to make a set? JS doesn't have native sets, exactly... but you can get the same functionality (meaning no duplicate items) with objects.

var setObject = {};

var choices = ['foo', 'foo', 'bar', 'baz', 'baz'];

choices.forEach(function(choice){
  setObject[choice] = true;
});

console.log(setObject); // >>> {foo: true, bar: true, baz: true}
posit labs
  • 8,951
  • 4
  • 36
  • 66