Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Array vielleicht?

    NEWS

    • Wir empfehlen: Node.js 22.x

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker goes Matter ... Matter Adapter in Stable

    UNSOLVED Array vielleicht?

    This topic has been deleted. Only users with topic management privileges can see it.
    • Lenny.CB
      Lenny.CB Most Active last edited by Bluefox

      Hallo zusammen,

      ich habe mal wieder ein Problem, dass ich allein nicht lösen kann.

      ich habe ein JSON aus dem ich ein paar Zeiten hole (naja, sind ja noch keine richtigen Zeiten :shock: ).

      Hier mal das Beispiel. Das JSON ist wegen der "Übersichtlichkeit" eingekürzt.

      var body = '{"testJSON":{"state":{"noonTime":[1,50],"eveningTime":[2,45],"tomorrowTime":[12,30]}}}';
      
      var obj = JSON.parse(body);
          for (i=0; i<(Object.keys(obj).length); i++){
              log(obj[Object.keys(obj)[i]].state.noonTime);
              log(obj[Object.keys(obj)[i]].state.eveningTime);
              log(obj[Object.keys(obj)[i]].state.tomorrowTime);
      
              var noonTime_hh = obj[Object.keys(obj)[i]].state.noonTime[0];
              var noonTime_mm = obj[Object.keys(obj)[i]].state.noonTime[1];          
              var noonTime = ((noonTime_hh > 9) ? noonTime_hh = noonTime_hh : noonTime_hh = '0' + noonTime_hh) + ':' + ((noonTime_mm > 9) ? noonTime_mm = noonTime_mm : noonTime_mm = '0' + noonTime_mm);
              log(noonTime);
          }
      

      Nun habe ich den unteren Teil für jede einzelne Zeit geschrieben. Das sind bei 10 Zeiten im JSON viel Code. Ich wollte aufräumen und einkürzen, da dachte ich "man kann sicher zusammen fassen". Ich hatte ein Array im Kopf, bekomme es aber nicht hin.

      Frage: wie kann ich das "rote" variabel machen?

      var noonTime_hh = obj[Object.keys(obj)_~~[i]~~].state.noonTime[0];
      
      var noonTime_mm = obj[Object.keys(obj)_~~[i]~~].state.noonTime[1];
      

      Grüße, Lenny__

      1 Reply Last reply Reply Quote 0
      • Lenny.CB
        Lenny.CB Most Active last edited by

        Keiner ne Idee wie ich das anstellen könnte?

        1 Reply Last reply Reply Quote 0
        • Bluefox
          Bluefox last edited by

          @Lenny-CB said in Array vielleicht?:
          Es ist nicht ganz klar, was bei dir nicht funktioniert, weil dein Code sieht schon OK aus, (außer, dass sehr ineffektiv)

          var body = '{"testJSON":{"state":{"noonTime":[1,50],"eveningTime":[2,45],"tomorrowTime":[12,30]}}}';
          
          var obj = JSON.parse(body);
          const padding = num => num > 9 ? num : '0' + num;
          
          Object.keys(obj).forEach(attr => {
              const state = obj[attr].state;
              console.log(state.noonTime);
              console.log(state.eveningTime);
              console.log(state.tomorrowTime);
          
          	console.log(`${padding(state.noonTime[0])}:${padding(state.noonTime[1])}`);
          	console.log(`${padding(state.eveningTime[0])}:${padding(state.eveningTime[1])}`);
          	console.log(`${padding(state.tomorrowTime[0])}:${padding(state.tomorrowTime[1])}`);
          });
          
          
          Lenny.CB 1 Reply Last reply Reply Quote 0
          • paul53
            paul53 last edited by paul53

            Versuche es mal so:

            var body = '{"testJSON":{"state":{"noonTime":[1,50],"eveningTime":[2,45],"tomorrowTime":[12,30]}}}';
            
            var obj = JSON.parse(body).testJSON.state;
            var keys = Object.keys(obj);
            var vals = Object.values(obj);
            
            for(let i = 0; i < keys.length; i++) {
                var h = vals[i][0];
                if(h < 10) h = '0' + h;
                var m = vals[i][1];
                if(m < 10) m = '0' + m;
                log(keys[i] + ' ' + h + ':' + m);
            }
            
            
            AlCalzone 1 Reply Last reply Reply Quote 1
            • AlCalzone
              AlCalzone Developer @paul53 last edited by

              @paul53 said in Array vielleicht?:

              Versuche es mal so:

              Oder wenn Object.values nicht existiert (Node < 8), dann so:

              // ...
              for(let i = 0; i < keys.length; i++) {
                  var h = obj[keys[i]][0];
                  // ...
              }
              1 Reply Last reply Reply Quote 0
              • Lenny.CB
                Lenny.CB Most Active last edited by

                ich probiere...

                1 Reply Last reply Reply Quote 0
                • Lenny.CB
                  Lenny.CB Most Active @Bluefox last edited by Lenny.CB

                  @Bluefox: Es ist nicht ganz klar, was bei dir nicht funktioniert, weil dein Code sieht schon OK aus,

                  also dein Code geht schon mal und verkürzt meinen pro Zeit auf 3 Codezeilen.

                  was wollte ich machen:
                  Zeile1: Stunden abfragen
                  Zeile2: Minuten abfragen
                  Zeile3: Zeit zusammenfassen und bei Einstelligkeit eine "0" davor

                  nun habe ich dennoch bei ca. 10 Zeiten 10 mal die Code-Zeile:

                  console.log(`${padding(state.noonTime[0])}:${padding(state.noonTime[1])}`);
                  

                  ich hatte halt gedacht, das man das nur einmal variabel schreiben braucht, etwa wie:

                  console.log(`${padding(state.[j][0])}:${padding(state.[j][1])}`);
                  

                  das [j] soll finden: noonTime, eveningTime, tomorrowTime
                  und das [0] bzw. [1] die Stunden/Minuten
                  Muss dann natürlich noch die entsprechende for-Schleife rein.

                  @Bluefox: (außer, dass sehr ineffektiv)

                  kannst du das mal verdeutlichen?

                  Bluefox 1 Reply Last reply Reply Quote 0
                  • Bluefox
                    Bluefox @Lenny.CB last edited by

                    @Lenny-CB @AlCalzone hat schon den richtigen code gepostet:

                    var body = '{"testJSON":{"state":{"noonTime":[1,50],"eveningTime":[2,45],"tomorrowTime":[12,30]}}}';
                    
                    var obj = JSON.parse(body);
                    const padding = num => num > 9 ? num : '0' + num;
                    
                    Object.keys(obj).forEach(attr => {
                        const state = obj[attr].state;
                    
                        Object.keys(state).forEach(name =>
                            console.log(`${name} - ${padding(state[name][0])}:${padding(state[name][1])}`));
                    });
                    

                    außer, dass sehr ineffektiv

                    Du liest 6 mal Object.keys(obj) und davon 5 mal im Zyklus.

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

                    Support us

                    ioBroker
                    Community Adapters
                    Donate

                    847
                    Online

                    32.0k
                    Users

                    80.4k
                    Topics

                    1.3m
                    Posts

                    javascript
                    4
                    8
                    518
                    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