jquery版本的array.contains [英] jquery version of array.contains

查看:97
本文介绍了jquery版本的array.contains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery是否可以测试数组是否存在对象(作为核心功能的一部分或通过可用的插件)?

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

此外,我正在寻找类似array.remove的东西,它将从数组中删除给定的对象. jQuery可以帮我解决这个问题吗?

Also, I'm looking for something like array.remove, which would remove a given object from an array. Can jQuery handle this for me?

推荐答案

jQuery.inArray 返回与您搜索的项目匹配的第一个索引;如果找不到,则返回-1.

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");

您不需要array.remove.使用接头:

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

theArray.splice(startRemovingAtThisIndex, numberOfItemsToRemove);

或者,您可以使用 jQuery.grep 实用工具执行删除":

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

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

这篇关于jquery版本的array.contains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆