/** * 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);
})();