Extjs中的迭代方法
EXTJS 有很多的迭代方法,例如,你也许已知道的Ext.each,但还有另外一些不为人知且很有用的方法。首先,简要回顾下Ext.each: Ext.each为每一个数组的成员应用同一个方法,它基本上是一个更方便的循环形式 var people = ['Bill', 'Saul', 'Gaius']; //using each to detect Cylons: Ext.each(people, function (person, index) { var cylon = (index + 1) % 2 == 0; //every second man is a toaster alert(person + (cylon ? ' is ' : ' is not ') + 'a fraking cylon'); }); //is the same as for (var i = 0; i < people.length; i++) { var person = people[i]; var cylon = (index + 1) % 2 == 0; //every second man is a toaster alert(person + (cylon ? ' is ' : ' is not ') + 'a frakin cylon'); }; Ext.iterate Ext.iterate 与 Ext.each 类似针对非数组对象. 通常用在for-in 循环中: var ships = { 'Bill': 'Galactica', 'Laura': 'Colonial One' }; Ext.iterate(ships, function (key, value) { alert(key + "'s ship is the " + value); }); //is the same as for (key in ships) { var value = ships[key]; alert(key + "'s ship is the " + value); } 用Ext.iterate在数组上,与Ext.each完全相同。 var myFunction = function (item, index) { //does some clever thing } Ext.each(people, myFunction); Ext.each(['another', 'array'], myFunction); Ext.pluck (4.0.0之后过时) Ext.pluck从对象数组捕获特定的属性 var animals = [ { name: 'Ed', species: 'Unknown' }, { name: 'Bumble', species: 'Cat' }, { name: 'Triumph', species: 'Insult Dog' } ]; Ext.pluck(animals, 'species'); //returns ['Unknown', 'Cat', 'Insult Dog'] Ext.pluck(animals, 'name'); //returns ['Ed', 'Bumble', 'Triumph'] 此方法自4.0.0不建议使用,请用Ext.Array.pluck代替. Ext.invoke(4.0.0之后过时)数组中所有成员调用同一个方法,并返回结果,使用用上例animals: var describeAnimal = function (animal) { return String.format("{0} is a {1}", animal.name, animal.species); } var describedAnimals = Ext.invoke(animals, describeAnimal); console.log(describedAnimals); // ['Ed is a Unknown', 'Bumble is a Cat', 'Triumph is a Insult Dog']; Ext.invoke与Ruby的集合方法类似,使得更容易转换数组,任何增加的参数都可通过Ext.invoke传递。 Ext.Partition将数组拆分成两部分。 var trees = [ { name: 'Oak', height: 20 }, { name: 'Willow', height: 10 }, { name: 'Cactus', height: 5 } ]; var isTall = function (tree) { return tree.height > 15 }; Ext.partition(trees, isTall); //returns: [ [{ name: 'Oak', height: 20}], [{ name: 'Willow', height: 10 }, { name: 'Cactus', height: 5}] ] 此方法自4.0.0不建议使用,4.X系列版本后将被移除。 数学方法var numbers = [1, 2, 3, 4, 5]; Ext.min(numbers); //1 Ext.max(numbers); //5 Ext.sum(numbers); //15 Ext.mean(numbers); //3 原文地址:Ext JS iterator functions 【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。 |
-
在 CSS 中使用中文字体通常直接设置字体名称,比如设...[详细]
-
镇江站长网 (http://www.0511zz.com)指出前各大运营...[详细]
-
在 WordPress 博客上,显示每个留言者的留言总数,效...[详细]
-
Zen Coding 一个用来简化编写 HTML,XML, XSL (或是...[详细]
-
一款灰色风格的CSS下拉菜单,响应鼠标滑过弹出效果,...[详细]
-
CSS橙色水平的导航菜单,代码不多、简洁实用。用到了...[详细]
-
基于 jQuery 的导航栏,鼠标点击后展开隐藏的列表内容...[详细]
-
一款普通的Tab网页选项卡切换,js+css代码。也可以叫...[详细]
-
以前我接受了网上不少博文的说法,一直认为学习css的...[详细]
-
导言 在这篇文章中我们将讨论一个与执行上下文直接相...[详细]