Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. gelöst-JS-Inhalt einer Variablen als Variable JSON

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    gelöst-JS-Inhalt einer Variablen als Variable JSON

    This topic has been deleted. Only users with topic management privileges can see it.
    • liv-in-sky
      liv-in-sky last edited by liv-in-sky

      hätte eine frage .bzw bitte

      ich habe eine json-variable, die man so abfragt:

      var x = resp.data[0].hostname
      var x= resp.data[0].name

      ich wurde gerne das ".hostname" oder ".name" variable halten - also abhängig von einer dritten variablen die auf true oder false gesetzt wird
      -> ist diese dritte variable true brauche ich : resp.data[0].name
      ->ist diese dritte variable falsch brauche ich : resp.data[0].hostname

      wie geht sowas ?

      Mic 1 Reply Last reply Reply Quote 0
      • Mic
        Mic Developer @liv-in-sky last edited by Mic

        @liv-in-sky
        Zunächst: ich denke wir sollten Variablen immer per let deklarieren, und nicht mehr per var, macht das Debugging etc. viel einfacher: https://love2dev.com/blog/javaScript-var-let-const/
        Das Block-Level-Scoping dank let hat mich schon vor einigen Fehlern bewahrt.

        Zu deiner Frage:
        Du verwendest die Punkt-Notation. Genau so kannst du das auch so machen:

        // Alternative A
        log(resp.data[0].hostname);
        // Alternative B
        let myVar = 'hostname';
        log(resp.data[0][myVar]);
        
        liv-in-sky 1 Reply Last reply Reply Quote 1
        • liv-in-sky
          liv-in-sky @Mic last edited by

          @Mic

          danke für deine antwort - war nicht weit weg von der lösung - ich hatte noch punkt drin - und damit gehts nicht

          // Alternative A
          ........
          log(resp.data[0].[myVar]);
          

          vielen dank nochmals- funktioniert im ersten schnelltest

          1 Reply Last reply Reply Quote 1
          • First post
            Last post

          Support us

          ioBroker
          Community Adapters
          Donate

          782
          Online

          31.7k
          Users

          79.9k
          Topics

          1.3m
          Posts

          javascript
          2
          3
          338
          Loading More Posts
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes
          Reply
          • Reply as topic
          Log in to reply
          Community
          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
          The ioBroker Community 2014-2023
          logo