简体   繁体   中英

Javascript: Getting a key value from an object function

Task is to get one of the key values from the object function to print to the console but having a spot of bother.

var chosenColor = function getThingsByColor(color){

  var things = {

    red: "This is a red thing",
    blue: "This is a blue thing",
    orange: "This is an orange thing"

  };

  return things[color] || "Sorry no color found";

}

coonsole.log(chosenColor('red'));

Result is red undefined.

在此处输入图片说明

algorithm is fine. coonsole => console ?

Your function works fine. I'm assuming you don't see the result printed out in the console. Make sure you have the 'All' tab selected for Console in your Dev Tools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM