Wie benutze ich eine Variable als JS -Objekttaste bei der Suche? [Duplikat]JavaScript

Javascript-Forum
Anonymous
 Wie benutze ich eine Variable als JS -Objekttaste bei der Suche? [Duplikat]

Post by Anonymous »

Ich habe ein einfaches Programm, das ich teste, wo ich die Eingabe -ID eines Benutzers nehme und diese verwende, um Daten von einem JS -Objekt zu sammeln. Ich kann jedoch nicht herausfinden, wie ich eine Variable als Suche danach verwenden kann.

Code: Select all

...

...




...
Script.js
var root = {
data:{
7292:{
...
},
8323:{
...
}
}
var display = document.getElementByID("displayTest");
var input = document.getElementByID("inputTextbox");
function submitID() {
if ((input.value) in (root.data)) {
display.innerText = root.data/*.the user's input.somedata*/;
} else {
display.innerText = "failed :(";
}
}
< /code>
Es ist wichtig, dass ich den Schlüssel in einer solchen Suche (root.data.Users input.somedata) erhalten kann, damit ich unter der Eingabe -ID auf Daten zugreifen kann. Zum Beispiel name....
var inputID = input.value;
function submitID() {
...
display.innerText = root.data.inputID.somedata; /*Doesn't return anything, I believe it's looking for a key called 'inputID'*/
...
}
...
< /code>
...
function submitID() {
...
display.innerText = root.data.${inputID}.somedata; /*Identifier Expected syntax error*/
...
}
...
< /code>
...
function submitID() {
...
display.innerText = root.data.(input.value).somedata; /*Identifier Expected syntax error*/
...
}
...
< /code>
I can't think of any other way to do this, and searching the web has been to no avail, so I turn to you, stackoverflow community, to help me.
Thanks in advance!
TDLR: I am trying to use a variable input as the key for a search in a js object
The code I wrote here is handwritten (not copy/pasted, so it may differ from the actual project), and is written on a device I am unfamiliar with. Assume misspellings & common syntax errors are fixed in project.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post