Reputation: 1
I may be doing this very badly but I'm new to this! if I have an array that contains objects like this
var company = [H7 = {companyName:"company1"},F4 = {companyName:"company2"}]
If I get a reference as a string say "F4" is there any way I can go
myCompName = company "F4" companyName and get the result "company2"
I was trying to use inArray like this
myStand = $.inArray("F4", companyObjects)
myCompName = companyObjects[myStand].companyName
but this doesn't work and yet
myStand = $.inArray(F4, companyObjects)
myCompName = companyObjects[myStand].companyName
does work. Do I have my array set up wrong or is there a way to do this? Thank you Alex
Upvotes: 0
Views: 51
Reputation: 66660
Arrays are indexed by numbers what you actually do with this line
var company = [H7 = {companyName:"company1"},F4 = {companyName:"company2"}]
is this:
H7 = {companyName:"company1"}
F4 = {companyName:"company2"}
var company = [H7,F4]
if you want to access H7 and F4 you need to create another object:
var company = {H7: {companyName:"company1"},F4: {companyName:"company2"}};
and then you can do this:
$.each(company, function(i, comp) {
var myCompName = 'company "' + i + '" companyName and get the result "'+
comp['companyName'] + '"';
});
or
company['H7']['companyName']
or
company.H7.companyName
Upvotes: 2
Reputation: 825
How about trying this way, http://jsfiddle.net/CBxMt/?
Basically company variable is an object that holds your company data. You can still access elements with [] notation just like arrays.
Upvotes: 0