顯示具有 pop 標籤的文章。 顯示所有文章
顯示具有 pop 標籤的文章。 顯示所有文章

2022年10月31日 星期一

Array 及 Object 的宣告, 及新增元素, 取得元素值處理

 目的: Array 及  Object 的宣告, 及新增元素 取值處理

處理說明:  1>Array  及 Object 的宣告
                       var Tmp_Ary=[];      //宣告 Array   
                       var Tmp_Obj={};     //宣告 Object

                   2>Array 及 Object 設定值
                       var Tmp_len=0;
                       for (var i=1; i<=5;i++)
                       { 
                           Tmp_len=Tmp_Ary.push(i) //傳回目前 Tmp_Ary.length
                           //Tmp_Ary[Tmp_len-1]=整數,並非Object ,所以無法設定property(newSet)
                           Tmp_Ary[Tmp_len - 1].newSet = i;    //undefined
                        } 
                       
                        Tmp_Obj = {
                             name: "蔡聰進",
                             gender: "男",
                        };             
                        Tmp_Obj.newSet=1;  //新增  Object 的屬性,直接 Assing 即可 

                        Tmp_len=Tmp_Ary.push(Tmp_Obj) ;
                        //Tmp_Ary[Tmp_len-1] 為 Object ,所以可以設定 property(newSet2) , 取值也會正常
                        Tmp_Ary[Tmp_len-1].newSet2=2;  

                   3>Array 及 Object 取值 
                       var Tmp_Ary=[1,2,3,4,5] ;
                       var  Tmp_Obj={
                               name: "蔡聰進",
                               gender: "男",
                               newSet: 1,
                               }
                      var Tmp_Str="";
                       //取得  Array 元素值_1  for 
                      for (var i=0; i<=Tmp_Ary.length-1;i++)
                     { 
                         Tmp_Str="Tmp_Ary[i]="+Tmp_Ary[i];
                        console.log(Tmp_Str) ;
                      }
                      //取得  Object property值  for 
                     for (var prop in Tmp_Obj)
                     {
                        Tmp_Str="Tmp_Obj["+prop +"]="+Tmp_Obj[prop];
                        console.log(Tmp_Str) ;
                      }

                      //取得  Array元素值_2  Ext.each
                      Ext.each(Tmp_Ary, functon(item,index){
                        Tmp_Str="Tmp_Ary["+index+"]="+item;
                        console.log(Tmp_Str) ;
                      });


實際案例:
                    var Tmp_Ary = [];
                    var Tmp_Obj = {};
                    var cur_recs = Ext.getCmp('grid_Single').getView().getSelectionModel().getSelection();
                    var cur_rec;
                    if (cur_recs.length > 0) {
                        cur_rec = cur_recs[0];
                        Tmp_Ary.push(cur_rec);
                        cur_rec.newSet = 0;
                    }
                    console.log("cur_rec.newSet:", cur_rec.newSet);
            console.log("Tmp_Ary[Tmp_Ary.length-1].newSet:", Tmp_Ary[Tmp_Ary.length - 1].newSet);
                    console.log("0 Tmp_Ary.length:", Tmp_Ary.length);
                    //mysuccessalert("cur_rec.newSet:" + cur_rec.newSet.toString());                    
                    var Tmp_len;
                    var Tmp_Str = "";

                   //0.5
                    for (var i = 1; i <= 5; i++) {                     
                        Tmp_len = Tmp_Ary.push(i);
                        console.log("0.5 Tmp_len:", Tmp_len);
                        Tmp_Ary[Tmp_len - 1].newSet = i;
                        //因 Tmp_Ary[Tmp_len - 1]=整數,非 Object , 所以,newSet undefined
                        console.log("0.5  Tmp_Ary[Tmp_len - 1].newSet:", Tmp_Ary[Tmp_len - 1].newSet);
                    }
                    for (var i = 1; i <= 5; i++) {
                        var Tmp_Obj1 = {};
                        Tmp_Obj1.value = i;
                        Tmp_len = Tmp_Ary.push(Tmp_Obj1);
                        console.log("1 Tmp_len:", Tmp_len);
                        //因 Tmp_Ary[Tmp_len - 1]= Object , 所以,newSet 可存值
                        Tmp_Ary[Tmp_len - 1].newSet = i;    
                        console.log("1  Tmp_Ary[Tmp_len - 1].newSet:", Tmp_Ary[Tmp_len - 1].newSet);
                    }
                    console.log("1 Tmp_Ary.length:", Tmp_Ary.length);
     
                    //1.5
                    //item 為 Array 元素
                    Ext.each(Tmp_Ary,
                        function (item, index) {
                            console.log("1.5 index:", index);
                            Tmp_Str = " 1.5  Tmp_Ary[index]=" + Tmp_Ary[index];
                            console.log(Tmp_Str);
                            Tmp_Str = " 1.5  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                            console.log(Tmp_Str);
                    )  
 
                    //1.8     
                    for (var index in Tmp_Ary) {
                        Tmp_Str = " 1.8  index=" + index;
                        console.log(Tmp_Str);
                        Tmp_Str = " 1.8  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                        console.log(Tmp_Str);
                    }
                    
                    //1.9  
                    Tmp_Str = " 1.9  Tmp_Ary.length=" + Tmp_Ary.length;
                    console.log(Tmp_Str);                     
                    for (var index = 0; index < Tmp_Ary.length ; index++) {
                        Tmp_Str = " 1.9  index: " + index;
                        console.log(Tmp_Str);
                        Tmp_Str = " 1.9  Tmp_Ary[index].newSet=" + Tmp_Ary[index].newSet;
                        console.log(Tmp_Str);
                    }
                    Tmp_Obj = {
                        name: "蔡聰進",
                        gender: "男",
                    };                 
                    Tmp_Obj.newSet = 1;

                    //2  
                    for (var item in Tmp_Obj) {
                        Tmp_Str = "2 Tmp_Obj[" + item + "] is " + Tmp_Obj[item];
                        console.log(Tmp_Str);
                    }

輸出結果:
0 Tmp_Ary.length: 1
0.5 Tmp_len: 2
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 3
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 4
0.5 Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 5
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined
0.5 Tmp_len: 6
0.5  Tmp_Ary[Tmp_len - 1].newSet: undefined

1 Tmp_len: 7
1  Tmp_Ary[Tmp_len - 1].newSet: 1
1 Tmp_len: 8
1  Tmp_Ary[Tmp_len - 1].newSet: 2
1 Tmp_len: 9
1  Tmp_Ary[Tmp_len - 1].newSet: 3
1 Tmp_len: 10
1  Tmp_Ary[Tmp_len - 1].newSet: 4
1 Tmp_len: 11
1  Tmp_Ary[Tmp_len - 1].newSet: 5
1 Tmp_Ary.length: 11

1.5 index: 0
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=0
1.5 index: 1
1.5  Tmp_Ary[index]=1
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 2
1.5  Tmp_Ary[index]=2
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 3
1.5  Tmp_Ary[index]=3
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 4
1.5  Tmp_Ary[index]=4
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 5
1.5  Tmp_Ary[index]=5
1.5  Tmp_Ary[index].newSet=undefined
1.5 index: 6
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=1
1.5 index: 7
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=2
1.5 index: 8
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=3
1.5 index: 9
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=4
1.5 index: 10
1.5  Tmp_Ary[index]=[object Object]
1.5  Tmp_Ary[index].newSet=5

1.8  index=0
1.8  Tmp_Ary[index].newSet=0
1.8  index=1
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=2
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=3
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=4
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=5
1.8  Tmp_Ary[index].newSet=undefined
1.8  index=6
1.8  Tmp_Ary[index].newSet=1
1.8  index=7
1.8  Tmp_Ary[index].newSet=2
1.8  index=8
1.8  Tmp_Ary[index].newSet=3
1.8  index=9
1.8  Tmp_Ary[index].newSet=4
1.8  index=10
1.8  Tmp_Ary[index].newSet=5


1.9  Tmp_Ary.length=11
1.9  index: 0
1.9  Tmp_Ary[index].newSet=0
1.9  index: 1
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 2
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 3
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 4
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 5
1.9  Tmp_Ary[index].newSet=undefined
1.9  index: 6
1.9  Tmp_Ary[index].newSet=1
1.9  index: 7
1.9  Tmp_Ary[index].newSet=2
1.9  index: 8
1.9  Tmp_Ary[index].newSet=3
1.9  index: 9
1.9  Tmp_Ary[index].newSet=4
1.9  index: 10
1.9  Tmp_Ary[index].newSet=5

2 Tmp_Obj[name] is 蔡聰進
2 Tmp_Obj[gender] is 男
2 Tmp_Obj[newSet] is 1

2022年8月31日 星期三

JS Array 陣列宣告 - push,pop - unshift,shift

目的: 說明 陣列 (Array) 的宣告及使用

 陣列可以看作是一種特別的「物件」,同樣是零至多個元素的集合

陣列內可以是原始的資料類型、其他陣列、函式等等

陣列是個有順序性的集合,且只能透過 [] 來存取。

陣列的索引是由 0 開始計算的

一.陣列宣告的3個方法:

1>方法1:  new Array();

var a = new Array(); 
a[0] = "apple"; 
a[1] = "boy"; 
a[2] = "cat"; 
a.length; // 3

2>方法2:   []
var a = []; 
a[0] = "apple"; 
a[1] = "boy"; 
a[2] = "cat"; 
a.length; // 3

3>方法3:   ["apple","boy","cat"];
var a = ["apple", "boy", "cat"]; 
a.length; //3


二.陣列的新增/刪除

1>Array.legth 可以修改
var a = ["apple", "boy", "cat"]; 
a.length; // 3 
a.length = 1; 
console.log(a); // ["apple"] 
a.length = 3; 
console.log(a); // ["apple", undefined, undefined]

2>Array元素可以任意指定
var array = ['a', 'b', 'c']; 
array.length; // 3 
array[7] = 'z'; 
console.log(array); // ["a", "b", "c", undefined, undefined, undefined, undefined, "z"]

3>在陣列末端新增/移除元素時,可以透過 ARRAY.push() ; Array.pop()
var array = ['a', 'b', 'c']; 
array.length; // 3 
array.push('d'); 
console.log(array); // ['a', 'b', 'c', 'd']
array.pop();            //傳回 "d"
console.log(array); // ['a', 'b', 'c']

4>在陣列前端新增/移除元素時,可以透過 ARRAY.unshift() ; Array.shift()
var array=['a', 'b', 'c'];
var Tmp_el=array.shift();
console.log(array); // ['b','c']
Tmp_el=array.shift();
console.log(array); // ['c']
Tmp_el=array.unshift(4,"e");
console.log(array); // [4,"e",'c']