0

I am having the most difficult time trying to accomplish what to me, is a very straightforward task.

I currently have n external car dealer API that is called in the header of a html page. At the bottom of the page is another external .js file that has a list of if then else statements to show or hide various aspects of the page based on what type of car is passed down in the api.

The problem is that no matter what I do to get the key value, it always comes back undefined, yet if I call autoCarList in the console, I am given the following.

Object {Honda: "Civic", Ford: "Escort", Toyota: "Camry"}

There is another function that is called getautoCarList() and if I type in getautoCarList('Honda'); I will get Civic back.

What I don't understand is how do I get say, Honda's value of Civic into a variable and passed to my list of if/then else statements if carType =='Civic' then proceed accordingly.

I've tried something as simple as

var showCar = getAutoCarList('Honda'); 

and

showCar = autocarlist.Honda;

I get nothing.

Inside the getAutoCarList() they are already using getAutoCarList = JSON.parse(results.arg);

At this point I'm at a loss but have continued reading up on JSON, it may very well be that it's not possible but I wanted a second opinion, or third.

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
VanCoon
  • 422
  • 4
  • 20
  • Assign the object to a variable... `var cars = autoCarList()` then just use `var value = cars.Honda` – An0nC0d3r Nov 06 '15 at 20:04
  • I had not though of that but it is still not pulling the values down and displaying. Does it have to do with the fact that the page has already been loaded and the values pulled down after the page has already been parsed? $(function() { var cars = autoCarList(); var value = cars.Honda; // SHOW HONDA if(pageType == 'honda') { $('#showhonda').show(); } if(pageType == 'toyota') { $('#showtoyota').show(); } }); – VanCoon Nov 06 '15 at 20:21
  • you are assigning to `cars` and you are checking in the `if` for `pageType`. change one. – BenG Nov 06 '15 at 20:25
  • Don't use the same name `getAutoCarList` for the function and variable. – Barmar Nov 06 '15 at 20:47

2 Answers2

0

if

getautoCarList();

is returning the object

Object {Honda: "Civic", Ford: "Escort", Toyota: "Camry"}

then you can just do:-

var model = getautoCarList().Honda; //-> "Civic"

or assign the object, and call from that.

var models = getautoCarList();
var honda = models.Honda; //-> "Civic"
var ford = models.Ford; //-> "Escort"
var toyota = models.Toyota; //-> "Camry"

EDIT

After realise from the comments I read the question wrong, it sounds like you are calling the getAutoCarList('Honda'); and autocarlist.Honda; BEFORE The API call has been made or has returned. If the API has a success/complete function then put your code in there.

you could do this to see if this is the issue:-

$(function() { 
   setTimeout(function(){ 
      var car = getautoCarList('Honda'); 
      // SHOW HONDA 
      if(car == 'Civic') { 
         $('#showhonda').show(); 
      } 
      if(car == 'Camry') { 
         $('#showtoyota').show(); 
      } 
    }, 5000);
 });

This will call the getautoCarList and your if statement 5 seconds after the page loads.

BenG
  • 14,826
  • 5
  • 45
  • 60
  • It doesn't return the object. He wrote: I type in `getautoCarList('Honda');` I will get `Civic` back. – Barmar Nov 06 '15 at 20:51
  • Thank you for that, at least I know I'm now on the right track. No matter what I have tried, suggestions included, I continue to receive "undefined' back in the console or nothing showing on the page. Thank you for the suggestions and advice. – VanCoon Nov 06 '15 at 20:53
0

To get the function's value into a variable, just assign it:

var carType = getautoCarList("Honda");
if (carType == "Civic") {
    // do something
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you all for the creative input. I was able to use the setTimeout to effectively troubleshoot which helped quit a bit. I did come away with one last question and that is why, and this may be beyond the scope of this discussion, – VanCoon Nov 09 '15 at 19:11
  • I was not able to simply write out the following below and have it work. Could be the way I have other aspects of my .js loading perhaps? At any rate this gives me more motivation to continue, than you all again! $(function() { var car = getautoCarList('Honda'); if(pageType == 'pms') { $('#showac').show(); } if(car == 'Camry') { $('#showtoyota').show(); } }); – VanCoon Nov 09 '15 at 19:11
  • Are you using AJAX to load the car list? Maybe you're calling `getautoCarList` before the AJAX call returns. See http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron?newsletter=1&nlcode=97716%7c4ba7 – Barmar Nov 09 '15 at 19:51