是否有任何 jquery 功能可以以与 DOM 类似的方式查询多维数组?
问题是什么...
jQuery 是否有任何方法可以让您以与 DOM 类似的方式查询多维对象数组。
例如,给我一个多维数组中包含的对象列表,这些对象具有一些匹配的属性值 - 例如,其中 StartOfPeriod 大于指定日期,或者 name == “Ben Alabaster”
我想避免重新发明如果已经有东西的话,就用轮子。
What the question says...
Does jQuery have any methods that will allow you to query a mult-dimensional array of objects in a similar fashion as it does with the DOM.
So for instance, get me a list of objects contained within a multi-dimensional array having some matching property value - for instance where StartOfPeriod greater than a specified date or where name == "Ben Alabaster"
I'd like to avoid re-inventing the wheel if there's something already out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用选择器语法,但 jQuery 附带 $.grep 和 $.inArray,这对此很有用。
grep
返回与谓词匹配的新元素数组。inArray
返回第一个匹配元素的索引,即 -1。例如:这些类似于标准 ECMAScript 方法,
Array .filter
(类似于grep
)和Array.indexOf
(类似于inArray
); jQuery 实际上在可用的情况下使用 Array.indexOf。还有其他有用的 ECMAScript 方法,例如Array.every
(所有元素匹配)和Array.some
(至少一个匹配)。 MDC 具有可以添加到项目中的代码,因此这些代码可以在没有本机实现的浏览器中运行。You can't use selector syntax, but jQuery comes with $.grep and $.inArray, which can be useful for this.
grep
returns a new array of elements that match a predicate.inArray
returns the index of the first matching element, or -1. For instance:These are similar to the standard ECMAScript methods,
Array.filter
(simimlar togrep
) andArray.indexOf
(similar toinArray
); jQuery actually usesArray.indexOf
where available. There are also other useful ECMAScript methods, such asArray.every
(all elements matching) andArray.some
(at least one matching). MDC has code you can add to your project so these work in browsers that don't have native implementations.您可能会找到一个插件,但不在 jQuery 核心中。有一些有用的数组方法:
each
、unique
、inArray
。结合起来,您可以创建一些自定义的东西来满足您的需求。你正在寻找的更多的是一个带有xpath之类的遍历的集合库。原型有更多的数组方法。但仍然可能无法满足您开箱即用的确切需求。
我同意alex的观点,这样的库/扩展会很有趣。
You may find a plugin, but not in the jQuery core. There are a few helpful array methods:
each
,unique
,inArray
. In combination, you could create something custom to meet your needs.What you are searching for is more of a set library with xpath like traversal. Prototype has a much larger set of array methods. But still probably wouldn't meet your exact needs out of the box.
I agree with alex, such a library/extension would be interesting.
我刚刚写了这个..我认为它工作正常,但它肯定可以被清理:)
像这样使用它:
返回与成员值对匹配的对象数组(或子对象) 。在本例中,
foo
包含moo
和c3
,因为这两个对象都包含baz = 1
对。让函数看起来像一个 jQuery 选择器只是一个语法糖的问题。
I just wrote this.. I think it works properly but it could definitely get cleaned up :)
Use it like so:
Returns an array of object (or sub-objects) that match the member-value pair. In this case,
foo
contains bothmoo
andc3
since both of the objects contain abaz = 1
pair.Making the function look and feel like a jQuery selector is just a matter of syntactic sugar.