简体   繁体   English

如何将键添加到具有数组值的对象?

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

I was asked to add a key (named interests) to an object (named myObject). 我被要求向一个对象(名为myObject)添加一个键(名为兴趣)。 the key must have an array value. 键必须具有数组值。 here i post the code. 在这里,我发布代码。 but it is not working. 但它不起作用。

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']
};

or if you're trying to use the array already created: 或者,如果您尝试使用已创建的数组:

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

or to add it after myObject has been created: 或在创建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