jquery version of array.contains

前端 未结 4 1368
我在风中等你
我在风中等你 2021-01-30 12:26

Can jQuery test an array for the presence of an object (either as part of the core functionality or via an avaible plugin)?

Also, I\'m looking for something like a

4条回答
  •  温柔的废话
    2021-01-30 13:33

    jQuery.inArray returns the first index that matches the item you searched for or -1 if it is not found:

    if($.inArray(valueToMatch, theArray) > -1) alert("it's in there");
    

    You shouldn't need an array.remove. Use splice:

    theArray.splice(startRemovingAtThisIndex, numberOfItemsToRemove);
    

    Or, you can perform a "remove" using the jQuery.grep util:

    var valueToRemove = 'someval';
    theArray = $.grep(theArray, function(val) { return val != valueToRemove; });
    

提交回复
热议问题