原文地址:http://www.veryhuo.com/a/view/36701.html

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完全相同。
each和iterate方法都有第三个可选参数scope。
另一个有用的技巧是你可以更方便的重用相同的方法:

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传递。
此方法自4.0.0不建议使用,4.X系列版本后将被移除。

Ext.Partition

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

[转]Extjs中的迭代方法的更多相关文章

  1. JavaScript中数组迭代方法(jquery)

    var arr = [1,2,4,5,6]; //1.forEach(让数组中的每一项做一件事)arr.forEach(function(item,index){    console.log(ite ...

  2. JavaScript中数组迭代方法

    文章来源 : https://www.cnblogs.com/shuiyi/p/5058524.html

  3. javascript中五种迭代方法实例

    温习一下js中的迭代方法. <script type="text/javascript"> var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]; ...

  4. ExtJs中XTemplate使用(转)

    转自http://www.studyofnet.com/news/408.html 本文导读:XTemplate是Ext.Template扩展的新类,它支持高级功能的模板类,如自动数组输出.条件判断. ...

  5. JavaScript 之迭代方法

    前言:关于 JS 中为数组定义的迭代方法,我最开始是在<JavaScript高级程序设计>中学习的,然后...我并没有看懂,后来翻阅各个大佬的博客,稍微理解了那么一丢丢.以下就是我的一点见 ...

  6. Extjs中给同一个GridPanel中的事件添加参数的方法

    Extjs中给同一个GridPanel中的事件添加参数的方法: this.isUse = new Ext.Action({            text:'启用',            scope ...

  7. javaScript中的数组迭代方法

    ECMAScript5为数组定义了5个迭代方法. 每个方法都接收两个参数:要在每一项上运行的函数  和  (可选的)运行该函数的作用域对象. 传入这些方法中的函数会接收三个参数:数组项的值,该项在数组 ...

  8. Javascript中的迭代、归并方法

    迭代方法 在Javascript中迭代方法个人觉得尤为重要,在很多时候都会有实际上的需求,javascript提供了5个迭代方法来供我们操作,它们分别为: every() 对数组中的每一个项运用给定的 ...

  9. JS中数组的迭代方法和归并方法

    昨天总结的JavaScript中的数组Array方法 数组的迭代方法 ES5中为数组定义了5个迭代方法.每个方法都要接收两个参数:要在每一项上面运行的函数和(可选的)运行该函数的作用域对象---影响t ...

随机推荐

  1. 在Linux上yum安装运行Redis,只能安装2.4.10(主从)

    Installing Redis on CentOS 6.4 First, install the epel repo sudo rpm -Uvh http://download.fedoraproj ...

  2. Php廖雪峰教程学习与实战

    https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 目录 Python教程 Pyth ...

  3. spark运行模式

    一.Spark运行模式 Spark有以下四种运行模式: local:本地单进程模式,用于本地开发测试Spark代码; standalone:分布式集群模式,Master-Worker架构,Master ...

  4. notepad++ 语法高亮

    1. notepad++ 添加新语言语法高亮和加载插件 用notepad++已经很久了,很习惯用这个小东西做事情,简单方便,超实用的一款工具. 先说说在呢么添加对新的编程语言的支持吧, 添加新语言语法 ...

  5. centos 6&7 升级openssh

    1.查看现在的版本 # rpm -qa | grep openssh openssh-clients-6.6.1p1-22.el7.x86_64 openssh-server-6.6.1p1-22.e ...

  6. SharePoint 关于拓扑错误的解决方案

    Issue Topology报错信息:SharePoint Web Services Round Robin Service Load Balancer Event: EndpointFailure. ...

  7. SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

    In this post I will introduce a way how to run a script for backing up SharePoint data which could b ...

  8. SQL中实现SPLIT函数几种方法

    例1 代码如下 复制代码 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp ...

  9. CentOS7 安装java 环境 摘抄

    转http://www.diyhi.com/hostConfig.html 服务器环境配置 下面介绍全新安装的CentOS系统服务器安装配置商城软件服务环境的方法.演示主机操作系统为CentOS 7. ...

  10. 转 kafka 清理数据

    由于项目原因,最近经常碰到Kafka消息队列拥堵的情况.碰到这种情况为了不影响在线系统的正常使用,需要大家手动的清理Kafka Log.但是清理Kafka Log又不能单纯的去删除中间环节产生的日志, ...