Ext.each
each : function(array, fn, scope){ if(typeof array.length == "undefined" || typeof array == "string"){ array = [array]; } for(var i = 0, len = array.length; i < len; i++){ if(fn.call(scope || array[i], array[i], i, array) === false){ return i; }; } },
var array = [11, 22, 33, 44, 55, 66, 77]; var content = ""; Ext.each(array, function (item,index,allitems) { content += item + "||" + index +"||"+allitems+ "\n"; }); alert(content);
var content = ""; var obj = [{ name: "cola", age: 27 }, { name: "moka", age: 28}]; Ext.iterate(obj, function (item, index, allitems) { content += item.name + "||" + index + "||" + allitems[index].age + "\n"; }); alert(content);
Ext.iterate 针对非数组对象,在对数组对象时与Ext.each功能一样
var content = ""; var ships = { 'Bill': 'Galactica', 'Laura': 'Colonial One' }; Ext.iterate(ships, function (key, val) { content += key + "||" + val + "\n"; }); alert(content); var content2 = ""; for (k in ships) { content2 += k + "||" + ships[k] + "\n"; } alert(content2);
Ext.pluck 从一组对象数组中拽出某个属性返回新的形成的这个属性的数组
var content = ""; var animals = [{ name: 'Ed', species: 'Unknown' }, { name: 'Bumble', species: 'Cat' }, { name: 'Triumph', species: 'Insult Dog'}]; var array1 = Ext.pluck(animals, "name"); var array2 = Ext.pluck(animals, "species"); alert(array1 + "\n" + array2);
Ext.partition 将数组分成两部分
var content = "";var content2 = ""; var animals = [{ name: 'Ed', species: 'Unknown' }, { name: 'Bumble', species: 'Cat' }, { name: 'Triumph', species: 'Insult Dog'}]; content = Ext.partition(animals, function (obj) { return obj.name.length > 3; }); Ext.each(content, function (item) { Ext.each(item, function (obj) { content2 += obj.name + "||" + obj.species + "\n"; }); content2 += "==================================\n"; }); alert(content2);
数学方法
var content = ""; var array = [1, 2, 3, 4, 5, 6]; var min = Ext.min(array); var max = Ext.max(array); var sum = Ext.sum(array); var mean = Ext.mean(array); content = min + "||" + max + "||" + sum + "||" + mean; alert(content);