DEV Community

loading...

Discussion on: Is it possible to nest an array in a JavaScript object?

Collapse
1e4_ profile image
Ian

What have you tried so far?

const obj = {
    an_array: []
}

Should work

Collapse
morrisjohn profile image
Morris John Author

What I've been trying to do is

...

var yes = {
Name: "Morris",
Age: 21

no = [22, 11, 1998]

};

I've been trying to do the opposite of how you nest objects in Arrays. I'm using Colt Steele's course and he said it's possible to nest objects in arrays but he only showed the other way round.

Forgive me I'm just learning

Collapse
1e4_ profile image
Ian • Edited

Formatting requires 3 backticks (`) not apostrophe (') fyi.

And that code won't work as you are missing some commands, each item in an object must be separated by a comma, also there is no =, you use a colon (:)


var yes = {
name: "Morris",
age: 21,
no: [22, 11, 1998]
};

Thread Thread
morrisjohn profile image
Morris John Author

Thanks. I can't believe the difference between = & : almost made me pull my entire hair out.

I saw mistake was declaring the array within the object with an equal sign (=) instead of a colon(:).

Thanks man