【发布时间】:2011-09-23 03:09:13
【问题描述】:
问题
我需要查询作为对象数组返回的每个项目的一些属性
我们现在做什么
$('selector').each(创建文本数组或其他任何东西);
怎么了?
我们为什么要为每个需求迭代元素?
在 LINQ 中也是这样
IQueryable<Book>;
books.Select(b=> b.Text).ToArray();
// or another sample
books.Select(b=> new { b.Text , b.ISBN}).ToArray();
我们/我需要什么?
选择指定的元素属性作为对象数组而不重复元素 - 这称为延迟枚举
有可能吗?
我认为是的,因为现在我有一些想法。
I already know about 'map' and 'each' methods, beware that both of them will reiterate over selector items.So yes map will results what i want, but not exactly.
Also note that even C# that have more better performance than java-script is using this method that i'm asking for,
So this is a requirement for all web-developers not just me and that's exactly why i'm sharing the problem here to be solved.
研究结果
使用 jQuery 迭代元素是不可避免的,因为 jQuery 本身不会使用循环遍历文档元素来查找元素,直到对于伪类来说是必要的,所以为了在 jQuery 基础设施上实现延迟枚举,我们必须强制 jQuery 迭代项目,并且让它变慢。
我使用名为“select”的伪类实现了延迟枚举,并像 lambda 表达式一样传入 .net。这可以像 .net 一样很好地处理选择,但结果比 map 慢,每个 jQuery 方法最多 50 个%.最后答案是jQuery map方法。
【问题讨论】:
-
您是在问是否可以在 jQuery 中不进行迭代?还是您告诉我们这是可能的?
标签: javascript jquery linq performance css-selectors