Javascript 如何在数组中查找重复值并返回True或False?

Javascript 如何在数组中查找重复值并返回True或False?,javascript,jquery,Javascript,Jquery,我已经找遍了这方面的例子,但没有找到答案 $('.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

我已经找遍了这方面的例子,但没有找到答案

$('.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');
        }

    }

 });
$('.ac items')。打开(flexClick,函数(){
var$this=$(this);
var inputSelection=$this.parent().parent().find('input').val();
var currentField=$this.parent().parent().find('input');
var crewInputs=$('input[id^=“船员名单-”);
var crewChoices=新数组();

对于(i=0;i测试您的值是否已经在数组中。如果没有,则推送它

    var crewChoices = new Array ();
    for (i=0; i<crewInputs.length; i++) {
        if($.inArray( crewInputs[i].value, crewChoices ) != -1) {
                crewChoices.push(crewInputs[i].value);
        }
    }
var crewChoices=newarray();

对于(i=0;i您是否希望创建一个集合?确切地说,JS没有本机集合……但是您可以通过对象获得相同的功能(意味着没有重复项)

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}

您可以使用
inArray()
()来测试它是否已经存在。如果您不想使用jQuery,请使用indexOf进行彻底的回答,如果返回值为-1,则elt不在数组中。@rogMaHall感谢您提供了有用的链接。它没有出现在我的查询中。