Jquery如何通过数组中的属性查找对象
假设我有一组“目的”对象:(
//array of purpose objects:
var purposeObjects = [
{purpose: "daily"},
{purpose: "weekly"},
{purpose: "monthly"}
];
为了简单起见,我省略了其他属性)
现在我想要一种方法,如果找到匹配的目的名称,则返回特定的对象之一。
这不起作用:
function findPurpose(purposeName){
return $.grep(purposeObjects, function(){
return this.purpose == purposeName;
});
};
findPurpose("daily");
但它实际上返回一个空数组:
[]
我正在使用 JQuery 1.5.2。我也尝试过 $.each() 但没有运气。 显然,大多数 JQuery 方法都是为与 DOM 元素一起使用而设计的(例如 filter()
)。
关于如何实现这一点有什么想法吗?
Given I have an array of "purpose" objects:
//array of purpose objects:
var purposeObjects = [
{purpose: "daily"},
{purpose: "weekly"},
{purpose: "monthly"}
];
(for simplicity i am omitting other attributes)
Now I want to have a method that returns a specific one of the objects if a matching purpose name is found.
This is not working:
function findPurpose(purposeName){
return $.grep(purposeObjects, function(){
return this.purpose == purposeName;
});
};
findPurpose("daily");
but it actually returns an empty array:
[]
I am using JQuery 1.5.2. I have also tried with $.each() but with no luck.
Apparently, most JQuery methods are designed for usage with DOM elements (such as filter()
.
Any ideas on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您的数组实际上是一组 JQuery 对象,那么简单地使用 .filter() 方法怎么样?
If your array is actually a set of JQuery objects, what about simply using the .filter() method ?
另一种解决方案:
使用:
firstOrNull([{ a: 1, b: 2 }, { a: 3, b: 3 }], function(item) { return item.a === 3; });
该函数不会对数组中的每个元素执行(这对于大型数组很有价值)
One more solution:
Using:
firstOrNull([{ a: 1, b: 2 }, { a: 3, b: 3 }], function(item) { return item.a === 3; });
This function don't executes for each element from the array (it's valuable for large arrays)
我为我的角度应用程序创建了一个 util 服务。它有两个经常使用的功能。
例如你有对象。
首先递归地从对象获取值而不抛出未定义的错误。
{prop:{nestedProp1:{nestedProp2:某个值}}};
获得没有未定义检查的nestedProp2 2。
基于
[{prop: {nestedProp1: {nestedProp2: somevalue1}}}, {prop: {nestedProp1: {nestedProp2: somevalue2}}}] 的第二个过滤器数组;
使用nestedProp2=somevalue2 从数组中查找对象
用于当前问题的解决方案。注入UtilService并调用,
或者更高级
I have created a util service for my angular application. It have two function which use very often.
For example you have object.
First getting value from object recursively without throwing undefined error.
{prop: { nestedProp1: {nestedProp2: somevalue}}};
get nestedProp2 2 without undefined checks.
Second filter array on basis
[{prop: { nestedProp1: {nestedProp2: somevalue1}}}, {prop: { nestedProp1: {nestedProp2: somevalue2}}}];
Find object from array with nestedProp2=somevalue2
For solution for current question. inject UtilService and call,
Or more advanced
Javascript 有一个专门用于此目的的函数: Array。原型.find。例如,
将回调扩展到函数并不困难。然而,这与 IE 不兼容(部分与 Edge 兼容)。有关完整列表,请查看浏览器兼容性
Javascript has a function just for that: Array.prototype.find. As example
It not difficult to extends the callback to a function. However this is not compatible with IE (and partially with Edge). For a full list look at the Browser Compatibility
从 Array.find 的 polyfill Array.prototype.find 代码复制,并将数组添加为第一个参数。
您可以将搜索词作为谓词函数传递
copied from polyfill Array.prototype.find code of Array.find, and added the array as first parameter.
you can pass the search term as predicate function
不需要 jQuery。
JavaScript 数组有一个 find 方法,这样你就可以在一行中实现这一点:
示例
如果你需要 IE 兼容性,请导入此 polyfill 在您的代码中。
No need for jQuery.
JavaScript arrays have a find method, so you can achieve that in one line:
Example
If you need IE compatibility, import this polyfill in your code.
您应该在 grep 函数中传递对项目的引用:
示例
you should pass reference on item in grep function:
Example
我个人使用一个更通用的函数,适用于任何数组的任何属性:
您只需这样调用它:
I personally use a more generic function that works for any property of any array:
You just call it like this:
错误是您不能在 grep 中使用
this
,但必须使用对该元素的引用。这有效:返回:
The error was that you cannot use
this
in the grep, but you must use a reference to the element. This works:returns:
使用 Underscore.js findWhere 函数 (http://underscorejs.org/#findWhere):
每日
等于:这是一个小提琴: http://jsfiddle.net/spencerw/oqbgc21x/
要返回多个(如果您的数组中有更多),您可以使用
_.where(...)
Use the Underscore.js findWhere function (http://underscorejs.org/#findWhere):
daily
would equal:Here's a fiddle: http://jsfiddle.net/spencerw/oqbgc21x/
To return more than one (if you had more in your array) you could use
_.where(...)
最好、最快的方法是
Best, Fastest way is