Hello.
I’ve got this code:
const myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": [
"jack",
"blanket",
"hamper",
"tool box" : [
"hammer",
"screwdriver",
"Monkey spanner"
]
]
}
}
};
The part I’m having trouble with is this:
"trunk": [
"jack",
"blanket",
"hamper",
"tool box" : [
"hammer",
"screwdriver",
"Monkey spanner"
]
I’m trying to nest a new array
called “tool box” inside the “trunk” array
.
I can (kind of) see why it doesn’t work (that semi-colon is to do with objects
, not array
s), but can’t find a way of doing this.
Can any one point out what I should be doing? Or tell me whether it can actually be done (that is, have a titled array
within an array
)?!
You can put an array in another array, but that’s not exactly what you’re attempting here.
"tool box" : [
"hammer",
"screwdriver",
"Monkey spanner"
]
This is an object property with a key and a value, which you can’t put in as an array item.
Hi, yes, I mentioned that in my post.
Trying to figure out how to do it.
Just come up with this - it works (I can access e.g. ‘hammer’, ‘screwdriver’ etc) but feel it’s not quite there…What do you think?
"trunk": [
"jack",
"blanket",
"hamper",
["toolbox", [
"hammer",
"screwdriver",
"Monkey spanner"
]],
]
If you want toolbox
to be in an object, you can make the object be an item in the array.
// array of mixed types
let myArr = [
'string',
999,
['I', 'am', 'an', 'array'],
{ name: 'object property'},
true
]
Thanks but how would I access e.g. “hammer”?
In my example
"trunk": [
"jack",
"blanket",
"hamper",
["toolbox", [
"hammer",
"screwdriver",
"Monkey spanner"
]],
]
I can get it by e.g.:
const toolBoxContents = myStorage.car.outside.trunk[3][1][0]
console.log(toolBoxContents) /* hammer */
Can’t figure out a way to reference it from the object:
"trunk": [
"jack",
"blanket",
"hamper",
{
"toolbox": [
"hammer",
"screwdriver",
"Monkey spanner"
]
}
]
In that example, “hammer” would be trunk[3].toolbox[0]
.
I wouldn’t particuluarly suggest building a collection this way, and if this is an FCC challenge, I suspect that you misread a part of the instructions.
I see, thanks for your help!
Your suspicions are unfounded - didn’t misread anything - just playing around with code.
Glad to help. Playing around with the code is, IMHO, the best way to learn how it works. I just didn’t want you to get frustrated thinking that this convoluted lookup method was required for the challenge. Happy coding!
Thanks again!
As you say this wouldn’t be a way to build a collection, any pointers to the correct way of achieving my aim (which is to have a sub array with a title inside a parent array)?
Since array items are accessed by index, not by name, adding a name to a sub-array doesn’t really gain you anything.
// It isn't uncommon to have an array of objects:
const users = [
{ username: 'ArielLeslie', isAwesome: true },
{ username: 'najccforum', isAwesome: true },
{ username: 'SpammySpammer', isAwesome: false }
];
// Then you can iterate over it
for (let i = 0; i<=users.length; i++) {
if (users[i].isAwesome) {
console.log('Hooray for ' + users[i].username + '!');
}
}
// or you might have an object of arrays
const flavors = {
iceCream: ['chunky monkey', 'vermonty python'],
cookies: ['chocolate chip', 'snickerdoodle', 'peanut butter'],
salads: ['sadness']
};
// then you can look the items up by name
function printFlavors(foodType) {
console.log('The available flavors of ' + foodType + ' are: ' + flavors[foodType].join(' and '));
}
1 Like
{ username: 'najccforum', isAwesome: true }
Thanks for all your help!