javascript - 如何通过将用户输入与其键匹配来显示对象的值?

javascript - How to show value from object by matching user input to its key?

我想制作一个转换工具,将一个代码(用户输入)转换为另一个(预定义)。我决定使用 Javascript 对象作为代码的容器,我的函数将接受用户输入,这实际上是来自 javascript 对象的键,将它与代码容器中的键匹配,如果匹配是找到后,该函数会将值显示到警告框。

我做了一个代码,但是它不起作用。我试图找到解决方案,但现在我失败了。

这是我的代码:

$(document).ready(function() {
    $("#convert").click(function(){
        var GardinerToUnicodeCodePoint = {
                        "A1"    :"995328",
                        "A1A"   :"995329",
                        "A1B"   :"995330",
                        "A1C"   :"995331",
                        "A2"    :"995332",
                        "A2A"   :"995333",
                        "A3"    :"995334",
                        "A3A"   :"995335",
                        "A3B"   :"995336",
                        "A4"    :"995337",
                        "A4A"   :"995338",
                        "A4B"   :"995339",
                        "A4C"   :"995340",
                        "A4D"   :"995341",
                        "A4E"   :"995342",
                        "A5"    :"995343",
                        "A5A"   :"995344",
                        "A5B"   :"995345",
                        "A5C"   :"995346",
                        "A6"    :"995347",
        };
        var userInput = $("#userInput").val; /*for example 'A1'*/
        if (userInput in GardinerToUnicodeCodePoint) {
            alert(/*value of key 'userInput' -> 995328*/);
        } else {
            alert("No code found!");
        }
    });
});

调用对象后可以使用[]获取键值对:

GardinerToUnicodeCodePoint[userInput]

将您的代码更改为:

    var userInput = $("#userInput").val; /*for example 'A1'*/
    if (userInput in GardinerToUnicodeCodePoint) {
        alert(GardinerToUnicodeCodePoint[userInput]);
    } else {
        alert("No code found!");
    }

参见 jsfiddle:https://jsfiddle.net/wy70s3gj/

function getReturnCodeUsingKey(keyFromUserInput)
    {
        var GardinerToUnicodeCodePoint = {
            "A1"    :"995328",
            "A1A"   :"995329",
            "A1B"   :"995330",
            "A1C"   :"etc"
        };

        returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];

        return returnVal ? returnVal : "error: no match found";
    } 

从用户那里传递你的 string input 功能,它会 return 我想你想要的。

所以,您的完整解决方案如下所示:

$(document).ready(function() {
    $("#convert").click(function(){
        var userInput = $("#userInput").val(); /*for example 'A1'*/
        // a call to our new function keeping responsibilities seperated
        return getReturnCodeUsingKey(userInput);
    });
});

function getReturnCodeUsingKey(keyFromUserInput)
{
    var GardinerToUnicodeCodePoint = {
                    "A1"    :"995328",
                    "A1A"   :"995329",
                    "A1B"   :"995330",
                    "A1C"   :"995331",
                    "A2"    :"995332",
                    "A2A"   :"995333",
                    "A3"    :"995334",
                    "A3A"   :"995335",
                    "A3B"   :"995336",
                    "A4"    :"995337",
                    "A4A"   :"995338",
                    "A4B"   :"995339",
                    "A4C"   :"995340",
                    "A4D"   :"995341",
                    "A4E"   :"995342",
                    "A5"    :"995343",
                    "A5A"   :"995344",
                    "A5B"   :"995345",
                    "A5C"   :"995346",
                    "A6"    :"995347",
    };

    // set a variable to hold the return of the object query
    returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];

    //return valid value from object, or string if undefined
    return returnVal ? returnVal : "error: no match found";
} 

这里的问题正如 @epascarello 的评论中所表达的那样,您应该使用带有括号

$("#userInput").val();

代码示例:

$('#convert').click(function() {
  var GardinerToUnicodeCodePoint = {
    A1: '995328',
    A1A: '995329',
    A1B: '995330',
    A1C: '995331',
    A2: '995332',
    A2A: '995333',
    A3: '995334',
    A3A: '995335',
    A3B: '995336',
    A4: '995337',
    A4A: '995338',
    A4B: '995339',
    A4C: '995340',
    A4D: '995341',
    A4E: '995342',
    A5: '995343',
    A5A: '995344',
    A5B: '995345',
    A5C: '995346',
    A6: '995347'
  };
  var userInput = $('#userInput').val(); 
  var result = userInput in GardinerToUnicodeCodePoint
    ? 'Value of key \'userInput\' -> ' + userInput
    : 'No code found!';

  console.log(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="userInput">
<button id="convert">Submit</button>