2017-02-24 53 views
1

我想推一個陣列到另一個陣列,但其結果產生不正確的結果推項目的陣列typescrip與鍵值

let pusheditems:any[] = []; 
pusheditems.push(this.yesvalue); 
pusheditems.push(this.selectedtruck); 

後來,當我console.log(pusheditems)

我得到的數組鍵入

array(0->yes array, 1->select truck array) 

什麼找的是改變0,1的索引值是一樣的字符串,卡車

,所以我會期望得到

array(yes->yes array, truck->select truck array) 

我也曾嘗試

pusheditems.push({yes:this.yesvalue}); //adding yes 
pusheditems.push({truck:this.selectedtruck}); //adding truck 

但是,這並不工作

this.yesvalues and this.selectedtruck are also arrays 

的值,我需要什麼再添加

回答

3

你試圖實現的是創建對象,而不是數組。

你可以這樣做:

let pusheditems = {}; 
pusheditems[this.yesvalue] = this.selectedtruck; 
+1

感謝伊戈爾這個作品 –

2

在打字稿,陣列只能有類型的數字鍵。您需要使用Dictionary對象,如:

let pusheditems: { [id: string]: any; } = {}; // dictionary with key of string, and values of type any 
    pusheditems[this.yesvalue] = this.selectedtruck; // add item to dictionary 
+0

由於它的工作原理 –