2015-06-17 56 views
1

我被要求將一個鍵(名爲interest)添加到一個對象(名爲myObject)。該鍵必須具有數組值。我在這裏發佈代碼。但它不起作用。如何使用數組值添加對象的鍵?

var interests=[]; 
var myObject = { 
    name: 'Eduardo', 
    type: 'Most excellent', 

    // Add your code here! 
    interests:'reading', 
    interests:'singing' 
}; 
+2

'興趣: 'reading','singing']',或者如果你想添加一些東西給對象:'myObject.interests = ['reading','singing']'。 – Andy

回答

0
var interests = []; 
var myObject = { 
    name: 'Eduardo', 
    type: 'Most excellent', 
    interests: ['reading', 'singing'] 
}; 

,或者如果你正在嘗試使用已經創建數組:

var interests = ['reading', 'singing']; 
var myObject = { 
    name: 'Eduardo', 
    type: 'Most excellent', 
    interests: interests 
}; 

或將其添加myObject的創建後:

var myObject = { 
    name: 'Eduardo', 
    type: 'Most excellent' 
}; 
myObject.interests = interests; 
+0

非常感謝@chris charles,它正在工作。 –

+0

隨時接受我的回答 –

相關問題