是否有任何 jquery 功能可以以与 DOM 类似的方式查询多维数组?

发布于 2024-09-08 07:05:42 字数 195 浏览 3 评论 0原文

问题是什么...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦冥 2024-09-15 07:05:42

您不能使用选择器语法,但 jQuery 附带 $.grep$.inArray,这对此很有用。 grep 返回与谓词匹配的新元素数组。 inArray 返回第一个匹配元素的索引,即 -1。例如:

var matches = $.grep(array, function(el){
  return el.StartOfPeriod > 2000;
});

这些类似于标准 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:

var matches = $.grep(array, function(el){
  return el.StartOfPeriod > 2000;
});

These are similar to the standard ECMAScript methods, Array.filter (simimlar to grep) and Array.indexOf (similar to inArray); jQuery actually uses Array.indexOf where available. There are also other useful ECMAScript methods, such as Array.every (all elements matching) and Array.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.

稚气少女 2024-09-15 07:05:42

您可能会找到一个插件,但不在 jQuery 核心中。有一些有用的数组方法:eachuniqueinArray。结合起来,您可以创建一些自定义的东西来满足您的需求。

你正在寻找的更多的是一个带有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.

獨角戲 2024-09-15 07:05:42

我刚刚写了这个..我认为它工作正常,但它肯定可以被清理:)

function findMatchingObjects(obj, member, value){
   var final = new Array();
   var temp = new Array();
   for(var p in obj){
    if (typeof obj[p] == 'object' ) {
     temp = findMatchingObjects(obj[p], member, value);
     if (temp.length == 1)
      final.push(temp[0]);
    }
    if (p == member && obj[p] == value) {
     final.push(obj);
    }
   }
   alert(final.length);
   return final;
}

像这样使用它:

var moo ={baz: 1, boo: 2, bar:{c1: 3, c2: 4, c3:{t:true, f:false, baz:1}},d: 11};
var foo = findMatchingObjects(moo, "baz", 1);

// did it work?
console.log(foo);

返回与成员值对匹配的对象数组(或子对象) 。在本例中,foo 包含 mooc3,因为这两个对象都包含 baz = 1 对。

让函数看起来像一个 jQuery 选择器只是一个语法糖的问题。

I just wrote this.. I think it works properly but it could definitely get cleaned up :)

function findMatchingObjects(obj, member, value){
   var final = new Array();
   var temp = new Array();
   for(var p in obj){
    if (typeof obj[p] == 'object' ) {
     temp = findMatchingObjects(obj[p], member, value);
     if (temp.length == 1)
      final.push(temp[0]);
    }
    if (p == member && obj[p] == value) {
     final.push(obj);
    }
   }
   alert(final.length);
   return final;
}

Use it like so:

var moo ={baz: 1, boo: 2, bar:{c1: 3, c2: 4, c3:{t:true, f:false, baz:1}},d: 11};
var foo = findMatchingObjects(moo, "baz", 1);

// did it work?
console.log(foo);

Returns an array of object (or sub-objects) that match the member-value pair. In this case, foo contains both moo and c3 since both of the objects contain a baz = 1 pair.

Making the function look and feel like a jQuery selector is just a matter of syntactic sugar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文