ExtJS Array使用详细介绍

/**
 * extjs Array使用详细介绍
 */
(function() {

 Ext.onReady(function() {

  // 1
  var myArray = [1, 2, 3, 4, -3, -4];
  Ext.Array.every(myArray, function(item) {
     if (item > 0) {
      return true;
     } else {
      // alert(item);
      return false;
     }
    }, this);

  // 2
  var newArray = Ext.Array.filter(myArray, function(item) {
     if (item > 0) {
      return true;
     } else {
      return false;
     }
    }, this);
  // alert(newArray.join("\n"));

  // 3
  Object.prototype.get = function(key, defv) {
   if (this[key]) {
    return this[key];
   } else {
    if (defv) {
     return defv;
    }
   }
  }
  var person = {
   name : 'uspcat',
   age : 26
  }
   // alert(person['age']);
   // alert(person.get('name'))

  });

 // 4 Concatenating two or three arrays
 var alpha = ["a", "b", "c"];
 var numeric = [1, 2, 3];
 var num3 = [7, 8, 9];
 var alnum = alpha.concat(numeric);

 // alert(alnum)

 var alnu3 = alpha.concat(numeric, num3);
 // alert(alnu3);

 // 5 Concatenating values to an array

 var alphas = ['a', 'b', 'c'];

 var alnumeric = alphas.concat(1, [2, 3]);
 // alert(alnumeric);

 // 6 用一个字符串连接一个数组中所有元素
 var a = new Array("wind", "Rain", "Fire");
 var myVar1 = a.join();
 // alert(myVar1);
 var myVar2 = a.join(",");
 // alert(myVar2);
 var myVar3 = a.join("+");
 // alert(myVar3);

 // 7 The pop method removes the last element from an array and returns that
 // value to the caller.
 var myFish = ["angel", "clown", "mandarin", "surgeon"];

 var popped = myFish.pop();
 // alert(popped); //返回结果 surgeon

 // 8 Adds one or more elements to the end of an array and returns the new
 // length of the array.

 var sports = ["soccer", "baseBall"];
 var arrlen = sports.push("football", "swimming")
 // alert(arrlen);// 返回长度 4

 // 9数组元素翻转(顺序倒置)
 var myArray = ["one", "two", "three"];
 myArray.reverse();
 // alert(myArray);// 结果three,two,one

 // 10 移除第一个元素从一个数组,并返回那移除的那个元素

 var myFishs = ["angel", "clown", "mandarin", "surgeon"];

 // console.log("before: " + myFishs);

 var shifted = myFishs.shift();
 // console.log("after: " + myFishs);

 // console.log("the removed element: " + shifted);

 // 11数组元素的排序(从小到大)
 /*
  * function compareNumbers(a,b){ return a-b;
  */

 var numbers = [4, 2, 5, 3, 6, 0];
 numbers.sort(function(a, b) {
    return a - b;
   });
 // console.log(numbers);

 // 12 数组转字符串(Returns a string representing the array and its elements.
 // Overrides the Object.prototype.toString method.)
 var monthNames = new Array("Jan", "Feb", "Mar", "Apr");
 monthNames.toString();
// console.log(monthNames);

 // 13 Adds one or more elements to the front of an array and returns the new
 // length of the array.
 var myFishh = ["angel", "clown"];
 console.log("before:"+myFishh);
 var unshiftedd = myFishh.unshift("drum", "lion");
 console.log("after:"+myFishh);
 console.log("New length of array:"+unshiftedd);

})();
posted @ 2012-11-11 21:32  Kane_BJ  阅读(954)  评论(0编辑  收藏  举报