1

I have an array and I'm not sure how to access certain keys. What I'm trying to accomplish is simply target certain keys/values in the array. Here's a sample of my array.

var jobs = [ { // hunting name: 'Hunting', available: [ { name: 'Housemate', description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.', salary: 10 }, { name: 'Fetcher', description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.', salary: 15 }, { name: 'Hunter', description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.', salary: 25 }, { name: 'Elder', description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.', salary: 0 } ], // construction name: 'Construction', available: [ { name: 'Builder', description: 'You are a builder. You are the lowest class of the construction tier.', salary: 45 }, { name: 'Driver', description: 'You are a driver. You do the fetching and gathering of resources.', salary: 55 }, { name: 'Engineer', description: 'You are a engineer. You do the wiring and electrical work in the construction.', salary: 65 }, { name: 'Overseer', description: 'You are the overseer. You watch over the construction and give orders.', salary: 80 } ], } ]; 

Now keep in mind that I have multiple arrays in one array. Here I try to access the Hunter job category, the Fetcher job, and the construction Engineer salary.

alert(jobs.'Hunting'); // gives 'missing name after . operator' error alert(jobs.name[0]); // gives 'name is not defined' error alert(jobs.available.'Fetcher'); //same error as number 1 alert(jobs.available.salary[0]) // gives available is not defined error 

How can I access those variables?

12
  • Try jobs[0].name === 'hunting'. jobs is an array and the first item is an object. You can't select by the value of a property either. Commented May 14, 2015 at 13:46
  • Please post it as an answer, and explain how the code checks it as it kind of confuses me on how it grabs it Commented May 14, 2015 at 13:46
  • 1
    @whipdancer It took me a moment to notice, but "Hunting" and "Construction" are different top-level objects inside the array. It might not make sense to use those as key identifiers, so I think an array makes sense. Commented May 14, 2015 at 13:49
  • 3
    Could be wrong but it doesn't appear that the first object is ever closed meaning all of the "Construction" properties would overwrite the previous values. Am I missing something in the formatting? I don't see a second { and }... Commented May 14, 2015 at 13:50
  • 2
    Ah, yes! I was wrong; there should be a }, { at the // construction line. Commented May 14, 2015 at 13:55

3 Answers 3

2

Your array of objects is malformed

Your original array contained a single item: one object which had the name and available properties defined twice.

I suspect you want your array to contain two items: two objects, each with a name and available property.

It should be this:

var jobs = [ { // hunting name: 'Hunting', available: [ { name: 'Housemate', description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.', salary: 10 }, { name: 'Fetcher', description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.', salary: 15 }, { name: 'Hunter', description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.', salary: 25 }, { name: 'Elder', description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.', salary: 0 } ] }, { // construction name: 'Construction', available: [ { name: 'Builder', description: 'You are a builder. You are the lowest class of the construction tier.', salary: 45 }, { name: 'Driver', description: 'You are a driver. You do the fetching and gathering of resources.', salary: 55 }, { name: 'Engineer', description: 'You are a engineer. You do the wiring and electrical work in the construction.', salary: 65 }, { name: 'Overseer', description: 'You are the overseer. You watch over the construction and give orders.', salary: 80 } ], } ]; 

Accessing items in the array

alert(jobs[0].name); // Returns 'Hunting' alert(jobs[0].available[1].name); // Returns 'Fetcher' alert(jobs[0].available[3].salary); // Returns '0' 

Why don't the following examples work?

You can't use a string in dot notation:

alert(jobs.'Hunting'); alert(jobs.available.'Fetcher'); 

You cannot have a string after the dot. You should have a property name as in object.name, but you first need to define by its index which item in the array you're targeting as in array[i].name.

But even if you changed it to…

alert(jobs[0].Hunting); // OR alert(jobs[0]['Hunting']); 

…it would fail because there is no object with a property name of 'Hunting'.

Square brackets are misplaced:

alert(jobs.name[0]); alert(jobs.available.salary[0]); 

The above examples don't work because you are passing an index inside square brackets after your property name, where they should be placed after the array name. For example:

alert(jobs[0].name); alert(jobs[0].available[0].salary); 

Accessing objects in array by key/value

It looks like you're attempting to access the object's in the array by the value from one of its properties.

For example, above it seems you want to get the object whose property of name has a value of 'Hunting', which cannot be done directly.

You would need to create a function or use a library that provides a function for this, such as Underscore's _.find.

Example of using _.find to get an object by key/value:

var hunting = _.find(jobs, function(obj) { return obj.name === 'Hunting'; }); 

View the above examples in JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Love the explaination and examples. Thanks, gfullam!
2

I took the liberty of fixing a syntax error in you example. As you can see in the comments, there were close/opening braces missing between Hunter and Construction.

 }]}, // construction {name: 'Construction', 

You will need to use the index notation to get at the different elements in the array.

This will return the Hunter object. From there you can access the individual elements (either name or available).

console.log(jobs[0]); 

This will give you the name of the first object's name property.

console.log(jobs[0].name); 

This will return the first object under available.

console.log(jobs[0].available[0]); 

This will return the name property from the first object under available.

console.log(jobs[0].available[0].name); 

Here is a fiddle I created

2 Comments

You know, on jobs[0], available attribute are setup 2 times, if you use regular javascript the second will surcharge the first. (so the first one is deleted). If you are on strict javascript this will crash. So with this object you can't retrieve all the data
@Arthur I think that is the syntax (malformed array/object) referred to in the comments.
0

You didn't build your object correctly, do this :

var jobs = { // New child object create only for Hunting hunting: { // hunting name: 'Hunting', // optional available: [ { name: 'Housemate', description: 'You stick around the cabin of the hunters. You are the lowest class of the hunters.', salary: 10 }, { name: 'Fetcher', description: 'You are the fetcher of the clan. You gather leather, resources, and skin leather.', salary: 15 }, { name: 'Hunter', description: 'You are a basic hunter of the clan. You hunt for food, meat, and leather.', salary: 25 }, { name: 'Elder', description: 'You are a elder of the clan. You are respected among many, and may ask hunters for arrons.', salary: 0 } ] }, // Other section, this time for Construction construction : { // construction name: 'Construction', // Optional too available: [ { name: 'Builder', description: 'You are a builder. You are the lowest class of the construction tier.', salary: 45 }, { name: 'Driver', description: 'You are a driver. You do the fetching and gathering of resources.', salary: 55 }, { name: 'Engineer', description: 'You are a engineer. You do the wiring and electrical work in the construction.', salary: 65 }, { name: 'Overseer', description: 'You are the overseer. You watch over the construction and give orders.', salary: 80 } ], } }; 

Now you can do :

var construction_jobs = jobs.construction.available; 

If you absolutely want keep your array on first dimention you can do it :

var jobs = [ { // hunting name: 'Hunting', available: [ /* objects... */ ] }, { // construction name: 'Construction', available: [ /* objects... */ ] } ]; 

And use lodash lib to get data :

var job = _.findWhere(jobs, {name: 'Hunting'}); 

To understand, check your console log display on this codepen : http://codepen.io/ArthyFiciel/pen/waGxrm

3 Comments

Your 'if you absolutely want...' example appears to have the outermost {} and []s inverted.
@Arthur - I was thinking something similar, but, if he does it this way, he will lose any order to the collection of "jobs" (i.e., "Hunting", "Construction", etc.). If he were building a dropdown off of it, for example, and needed the options to be in order, his current structure would support that better.
@talemyn, on the question example you found 2 times the attribute name and available on the object. So if you do a console.log(jobs[0]). You will see only {name:'Construction', available: []} See here : codepen.io/ArthyFiciel/pen/waGxrm

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.