将字符串转换为JavaScript中的JSON键。

huangapple go评论64阅读模式
英文:

Convert a string into JSON key in JavaScript

问题

当用户在文本框中输入“b”时,如何将文本框中的字符串转换为用于访问JSON中“b”键的键值对应关系在JavaScript中是这样的:

var json = {
  "a": "letter a",
  "b": "letter b",
  "c": "letter c"
};

var userInput = "b"; // 用户在文本框中输入的值

var value = json[userInput]; // 使用用户输入作为键来访问JSON中的值

这样,变量value将包含JSON中键为“b”的值,“letter b”。

英文:

I have a JSON file that has:

{
"a": "letter a",
"b": "letter b",
"c": "letter c"
}

When the user type in the textbox "b", how do I convert the string from the textbox into a key to access the "b" key in the JSON in JS?

答案1

得分: 1

从用户首先获取值,然后再获取对象的值,可以尝试以下代码:

function myFunction() {
  var obj = { "a": "letter a", "b": "letter b", "c": "letter c" };
  var userInput = document.getElementById("user-input").value;
  if(userInput){
    console.log(obj[userInput] ?? "not found");
  }
}
Enter value: <input type="text" id="user-input" onkeyup="myFunction()">

请注意,这是 JavaScript 和 HTML 代码的翻译。

英文:

How about accessing the value from the user first and then accessing the value from object.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function myFunction() {
var obj = { "a": "letter a", "b": "letter b", "c": "letter c" };
var userInput = document.getElementById("user-input").value;
if(userInput){
console.log(obj[userInput] ?? "not found");
}
}

<!-- language: lang-html -->

Enter value: &lt;input type=&quot;text&quot; id =&quot;user-input&quot; onkeyup=&quot;myFunction()&quot;&gt;

<!-- end snippet -->

答案2

得分: 1

你可以使用方括号表示法来在JavaScript中访问JSON对象中特定键的值:

var value = jsonObject['userInput'];

例如:

const jsonObject = JSON.parse('{ "a": "letter a", "b": "letter b", "c": "letter c" }');

var userInput = document.getElementById('textbox').value; // userInput = 'b'

var value = jsonObject[userInput];

console.log(value); // 'letter b'
英文:

You can use the square bracket notation to access the value of a specific key in a JSON object in JavaScript:

var value = jsonObject[&#39;userInput&#39;];

For example:

const jsonObject = JSON.parse(&#39;{ &quot;a&quot;: &quot;letter a&quot;, &quot;b&quot;: &quot;letter b&quot;, &quot;c&quot;: &quot;letter c&quot; }&#39;);    

var userInput = document.getElementById(&quot;textbox&quot;).value; // userInput = &#39;b&#39;

var value = jsonObject[userInput];

console.log(value); // &#39;letter b&#39;

答案3

得分: 1

这是给定问题的解决方案:

应用程序结构:

将字符串转换为JavaScript中的JSON键。

app.js 文件

const textBox = document.getElementById("textbox");
const valueBox = document.getElementById("valuebox");
let data;
async function main() {
  await fetch("/data.json").then(async (v) => {
    data = await v.json();
  });
  console.log(data);
}
textBox.addEventListener("change", () => {
  let k = textBox.value;
  valueBox.innerHTML = data[k];
});
main();

index.html 文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input id="textbox" />
    <div id="valuebox"></div>
  </body>
  <script src="./app.js"></script>
</html>

data.json 文件

{ "a": "letter a", "b": "letter b", "c": "letter c" }
英文:

Here's the solution for a given problem:

App Structure:

将字符串转换为JavaScript中的JSON键。

app.js file

const textBox = document.getElementById(&quot;textbox&quot;);
const valueBox = document.getElementById(&quot;valuebox&quot;);
let data;
async function main() {
  await fetch(&quot;/data.json&quot;).then(async (v) =&gt; {
    data = await v.json();
  });
  console.log(data);
}
textBox.addEventListener(&quot;change&quot;, () =&gt; {
  let k = textBox.value;
  valueBox.innerHTML = data[k];
});
main();

index.html file


    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
      &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot; /&gt;
        &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
        &lt;title&gt;Document&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;input id=&quot;textbox&quot; /&gt;
        &lt;div id=&quot;valuebox&quot;&gt;&lt;/div&gt;
      &lt;/body&gt;
      &lt;script src=&quot;./app.js&quot;&gt;&lt;/script&gt;
    &lt;/html&gt;

data.json file

{ &quot;a&quot;: &quot;letter a&quot;, &quot;b&quot;: &quot;letter b&quot;, &quot;c&quot;: &quot;letter c&quot; }

huangapple
  • 本文由 发表于 2023年5月7日 11:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定