[轉]Extjs中的迭代方法

來源:互聯網
上載者:User

標籤:toast   cti   相同   ons   spec   func   code   參數   not   

原文地址: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 asfor (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 asfor (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); //1Ext.max(numbers); //5Ext.sum(numbers); //15Ext.mean(numbers); //3

原文地址:Ext JS iterator functions

[轉]Extjs中的迭代方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.