我被要求向一个对象(名为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;
关于javascript - 如何将键添加到具有数组值的对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30897297/