mayfair Posted November 30, 2010 Share Posted November 30, 2010 Hi Guys, I'm trying to sort some multidimensional arrays, but my JavaScript skills are somewhat lacking. Here is the scenario: I have an (undetermined) number of multidimensional arrays that store the exchange rates for a particular company that look something like the following: supplier['company1']['eur'] = 1.1111; supplier['company1']['usd'] = 2.2222; supplier['company1']['cad'] = 3.3333; supplier['company2']['eur'] = 1.1111; supplier['company2']['usd'] = 2.2222; supplier['company2']['cad'] = 3.3333; supplier['company3']['eur'] = 1.1111; supplier['company3']['usd'] = 2.2222; supplier['company3']['cad'] = 3.3333; What I would like to be able to do is find the company with the highest exchange rate for a particular currency out of these arrays by using the onChange event of a drop down list. The VALUE of the drop down list is the currency symbol, so I can basically use that when the function runs to set which currency to search for. So for example; if I select Euros from the drop down list, it will search the arrays for the company with the highest 'eur' rate, and return the company name and eur rate associated with it. Thanks in advance to anyone who might be able to help. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted November 30, 2010 Share Posted November 30, 2010 You're not actually using arrays in that example; those are objects. You will need to use the for...in construct to loop through the properties of the objects. Here are two functions which demonstrate this. The first, getMax(), returns an object with the company name and value for the maxiumum value. The second, getMaxes(), returns an array of similar objects, in case multiple companies tied for maximum value. var supplier = { company1:{}, company2:{}, company3:{} }; supplier['company1']['eur'] = 1.1111; supplier['company1']['usd'] = 2.2222; supplier['company1']['cad'] = 3.3333; supplier['company2']['eur'] = 4.4444; supplier['company2']['usd'] = 0.5555; supplier['company2']['cad'] = 6.6666; supplier['company3']['eur'] = 7.7777; supplier['company3']['usd'] = 2.2222; supplier['company3']['cad'] = 0.9999; function getMax(currency) { var co, max = Number.NEGATIVE_INFINITY, max_co; document.writeln(max); for (co in supplier) { document.writeln(co + " " + supplier[co][currency]); if (supplier[co][currency] > max) { max = supplier[co][currency]; max_co = co; } } return { value: max, company: max_co }; } var Max = getMax("eur"); document.writeln(Max.company + ": " + Max.value); function getMaxes(currency) { var co, max = Number.NEGATIVE_INFINITY, maxes = []; for (co in supplier) { if (supplier[co][currency] > max) { maxes = []; max = supplier[co][currency]; } if (supplier[co][currency] == max) maxes.push({ value: max, company: co }); } return maxes; } Max = getMaxes("cad"); for (var i = 0; i < Max.length; i++) document.writeln(Max[i].company + ": " + Max[i].value); These functions could be written better in order to re-use them with other objects/situations. One thing you could do is remove the hard-coded "supplier" object, replace it with "this", and use the Function.call method. function getMaxes(currency) { var co, max = Number.NEGATIVE_INFINITY, maxes = []; for (co in this) { if (this[co][currency] > max) maxes = [], max = this[co][currency]; if (this[co][currency] == max) maxes.push({ value: max, company: co }); } return maxes; } Max = getMaxes.call(supplier, "usd"); for (var i = 0; i < Max.length; i++) document.writeln(Max[i].company + ": " + Max[i].value); Quote Link to comment Share on other sites More sharing options...
mayfair Posted November 30, 2010 Author Share Posted November 30, 2010 Hi Wildbug, Many thanks for your reply and suggestions. I've implemented your code and it works nicely. -Mayfair Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.