Iteration Method in Extjs

Source: Internet
Author: User

EXTJS has many iteration methods, such as Ext. each, which you may already know, but there are other unknown and useful methods. First, let's briefly review Ext. each:

Ext. each

Apply the same method to members of each array. It is basically a more convenient loop form.

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. iterateExt. iterate is similar to Ext. each for non-array objects. It is usually used in a for-in loop:
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);}

Use Ext. iterate on the array, which is exactly the same as Ext. each.
The each and iterate methods both have the third optional parameter scope.
Another useful technique is that you can reuse the same method more easily:

var myFunction = function (item, index){    //does some clever thing}Ext.each(people, myFunction);Ext.each(['another', 'array'], myFunction);

Ext. pluck (outdated after 4.0.0) Ext. pluck captures specific attributes from the object Array
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']

This method is not recommended for 4.0.0. Use Ext. Array. pluck instead.

Ext. invoke

(Expired after 4.0.0) all the members in the array call the same method and return the result. Use the animals example above:

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 is similar to Ruby's collection method, making it easier to convert arrays. Any added parameter can be passed through Ext. invoke.
This method is not recommended for 4.0.0 and will be removed from 4.x series.

Ext. Partition

Ext. Partition splits the array into two parts.

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}]]

This method is not recommended for 4.0.0 and will be removed from 4.x series.

Mathematical Methods
var numbers = [1, 2, 3, 4, 5];Ext.min(numbers); //1Ext.max(numbers); //5Ext.sum(numbers); //15Ext.mean(numbers); //3

Original article address: Ext JS iterator functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.