簡體   English   中英

如何將鍵添加到具有數組值的對象?

[英]How to add a key to an object, with an array value?

我被要求向一個對象(名為myObject)添加一個鍵(名為興趣)。 鍵必須具有數組值。 在這里,我發布代碼。 但它不起作用。

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

  // Add your code here!
  interests:'reading',
  interests:'singing'
};
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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM