0

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.