简体   繁体   English

JavaScript 对象中的数组?

[英]Array inside a JavaScript Object?

I've tried looking to see if this is possible, but I can't find my answer.我试图看看这是否可能,但我找不到我的答案。

I'm trying to get the following to work:我正在尝试使以下内容起作用:

var defaults = {
 'background-color': '#000',
 color: '#fff',
 weekdays: {['sun','mon','tue','wed','thu','fri','sat']}
};

It just gives an error, and I've tried using ({...}) and [{...}] I'd like to be able to access the weekdays using something like:它只是给出了一个错误,我已经尝试使用({...})[{...}]我希望能够使用以下内容访问工作日:

defaults.weekdays[0];

is this possible?这可能吗?

Kill the braces.杀死大括号。

var defaults = {
 backgroundcolor: '#000',
 color: '#fff',
 weekdays: ['sun','mon','tue','wed','thu','fri','sat']
};
// define
var foo = {
  bar: ['foo', 'bar', 'baz']
};

// access
foo.bar[2]; // will give you 'baz'

 var data = { name: "Ankit", age: 24, workingDay: ["Mon", "Tue", "Wed", "Thu", "Fri"] }; for (const key in data) { if (data.hasOwnProperty(key)) { const element = data[key]; console.log(key+": ", element); } }

 var defaults = { "background-color": "#000", color: "#fff", weekdays: [ {0: 'sun'}, {1: 'mon'}, {2: 'tue'}, {3: 'wed'}, {4: 'thu'}, {5: 'fri'}, {6: 'sat'} ] }; console.log(defaults.weekdays[3]);

In regards to multiple arrays in an object.关于对象中的多个数组。 For instance, you want to record modules for different courses例如,您想记录不同课程的模块

var course = {
    InfoTech:["Information Systems","Internet Programming","Software Eng"],
    BusComm:["Commercial Law","Accounting","Financial Mng"],
    Tourism:["Travel Destination","Travel Services","Customer Mng"]
};
console.log(course.Tourism[1]);
console.log(course.BusComm);
console.log(course.InfoTech);

If you are so organised you may declare the entire object from the outset (this comma-delimited list is called an object initializer):如果你有条理,你可以从一开始就声明整个对象(这个逗号分隔的列表称为对象初始值设定项):

let myObject = {
    string:   'Galactic Rainbows',
    color:    'HotPink',
    sociopaths: ["Hitler","Stalin","Gates"]
}

Alternatively, once you have declared the object, you may define its properties by giving them values:或者,一旦你声明了对象,你可以通过给它们赋值来定义它的属性:

let myObject = {};    //this line is the declaration
myObject.string = "Galactic Rainbows";
myObject.color = "HotPink";
myObject.sociopaths = ["Hitler","Stalin","Gates"];

All examples below assume the object is already declared (as immediately above)下面的所有示例都假定该对象已被声明(如上所示)

I prefer to declare the array separately, like this, and then assign it to the object:我更喜欢像这样单独声明数组,然后将其分配给对象:

let weekdays = ['sun','mon','tue','wed','thu','fri','sat'];
myObject.weekdays = weekdays;

But if you have already declared the object, it would be quicker to code:但是如果你已经声明了对象,那么编码会更快:

myObject.weekdays = ['sun','mon','tue','wed','thu','fri','sat'];

But you cannot assign an array of arrays to the object like this:但是您不能像这样将数组数组分配给对象:

myObject.girlsAndBoys[0] = ["John","Frank","Tom"];    //Uncaught TypeError: Cannot set property '1' of undefined
myObject.girtsAndBoys[1] = ["Jill","Sarah","Sally"];   //Uncaught TypeError: Cannot set property '1' of undefined

To assign a two dimensional array to an object you have a few options.要将二维数组分配给对象,您有几个选择。 You can declare the empty 2D array first:您可以先声明空的二维数组:

myObject.girlsAndBoys    = [[]];
myObject.girlsAndBoys[0] = ["John","Frank","Tom"]; 
myObject.girtsAndBoys[1] = ["Jill","Sarah","Sally"];   

Or you may do it layer by layer:或者您可以逐层进行:

let boys  = ["John","Frank","Tom"];    
let Girls = ["Jill","Sarah","Sally"];
myObject.girlsAndBoys = [[boys],[girls]];

Alternatively you may do it all at once (after no more than the object declaration):或者,您可以一次完成所有操作(仅在对象声明之后):

let myObject = {};
myObject.girlsAndBoys = [["John","Frank","Tom"],["Jill","Sarah","Sally"]];
myObject.girlsAndBoys[0][0] == "John";   // returns True
var obj = {
 webSiteName: 'StackOverFlow',
 find: 'anything',
 onDays: ['sun'     // Object "obj" contains array "onDays" 
            ,'mon',
            'tue',
            'wed',
            'thu',
            'fri',
            'sat',
             {name : "jack", age : 34},
              // array "onDays"contains array object "manyNames"
             {manyNames : ["Narayan", "Payal", "Suraj"]}, //                 
           ]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM