Navigation

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

    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

    Zeiten filtern Array

    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      Diamand2k22 @ticaki last edited by Diamand2k22

      @ticaki

      Array würde so aussehen

      [[0.3659,"18:00","18:30"],[0.3659,"18:30","19:00"],[0.3657,"17:00","17:30"],[0.3657,"17:30","18:00"],[0.3631,"19:00","19:30"],[0.3631,"19:30","20:00"],[0.3559,"16:00","16:30"],[0.3559,"16:30","17:00"],[0.3553,"20:00","20:30"],[0.3553,"20:30","21:00"],[0.3486,"22:00","22:30"],[0.3486,"22:30","23:00"],[0.3454,"21:00","21:30"],[0.3454,"21:30","22:00"],[0.3433,"08:00","08:30"],[0.3408,"23:00","23:30"],[0.3408,"23:30","00:00"],[0.3356,"07:00","07:30"],[0.3356,"07:30","08:00"],[0.3232,"00:00","00:30"],[0.3232,"00:30","01:00"],[0.3194,"06:00","06:30"],[0.3194,"06:30","07:00"],[0.3148,"05:00","05:30"],[0.3148,"05:30","06:00"],[0.3147,"01:00","01:30"],[0.3147,"01:30","02:00"],[0.3142,"02:00","02:30"],[0.3142,"02:30","03:00"],[0.3097,"04:00","04:30"],[0.3097,"04:30","05:00"],[0.3083,"03:00","03:30"],[0.3083,"03:30","04:00"]]
      

      kann ich verstehen, wir können ja morgen nochmal weiterschauen 🙂

      vielleicht hat ja @paul53 noch eine Idee?

      T 1 Reply Last reply Reply Quote 0
      • T
        ticaki Developer @Diamand2k22 last edited by

        @diamand2k22

        async function filterZeitToSunup(arrZeit, sunup) {
           const newArray = [];
         
           // console.warn(JSON.stringify(arrZeit));
          
            for (const a of arrZeit ) {
               if (a[1] < sunup || a[1] > sundown) {
                    newArr.push(a)
                }
          
            // console.warn(JSON.stringify(newArray));
          
            newArray.sort(function (a, b) {  // niedrieg preis sort
                return b[0] - a[0];
            });
          
            //console.warn(JSON.stringify(newArray));
          
            return newArray;
         }
        

        So funktioniert es, alles was kleiner als sunup und größer als sundown im Feld 1 ist, wird ins neue Array geschrieben.

        1 Reply Last reply Reply Quote 0
        • D
          Diamand2k22 last edited by

          @ticaki

          Hab’s hinbekommen!
          danke für deine schnelle Hilfe!

          Grüße

          D 1 Reply Last reply Reply Quote 0
          • D
            Diamand2k22 @Diamand2k22 last edited by

            @paul53

            Hallo Paul, mal wieder eine Frage. Wie kann ich aus dem Beispiel Array unten den geringsten Preis und die volle Stunde mit dem geringsten Preis in zwei separate Datenpunkte schreiben. Wie kann ich das Array splitten?
            Also einmal das Array

            const arr = [ [ 0.2425, '03:00', '03:30' ], [ 0.2425, '03:30', '04:00' ], [ 0.2434, '02:00', '02:30' ], [ 0.2434, '02:30', '03:00' ], [ 0.2492, '04:00', '04:30' ] ];
            

            Datenpunkte wären

            Günstigster Preis
            Günstigste Zeit

            OliverIO paul53 2 Replies Last reply Reply Quote 0
            • OliverIO
              OliverIO @Diamand2k22 last edited by

              @diamand2k22

              hier mal eine superkompakte version
              wenn der wert gleich ist, wird immer das erste element genommen

              let [value,hour] = arr.reduce((acc,val)=>val[0]<acc[0]?val:acc);
              console.log(value);
              console.log(hour);
              
              1 Reply Last reply Reply Quote 0
              • paul53
                paul53 @Diamand2k22 last edited by

                @diamand2k22 sagte: geringsten Preis und die volle Stunde mit dem geringsten Preis

                const arr = [ [ 0.2425, '03:00', '03:30' ], [ 0.2425, '03:30', '04:00' ], [ 0.2434, '02:00', '02:30' ], [ 0.2434, '02:30', '03:00' ], [ 0.2492, '04:00', '04:30' ] ];
                const idPrice = ''; // ID eintragen
                const idHour  = ''; // ID eintragen
                
                let minPrice = 99;
                let hour = 24;
                for(const ele of arr) {
                    if(ele[0] < minPrice) {
                        minPrice = ele[0];
                        hour = parseInt(ele[1]);
                    }
                };
                setState(idPrice, minPrice, true);
                setState(idHour, hour, true);
                
                D 1 Reply Last reply Reply Quote 0
                • D
                  Diamand2k22 @paul53 last edited by Diamand2k22

                  @paul53

                  danke dir klappt soweit. Wie kann ich am einfachsten in Java umsetzen, dass er den Datenpunkt nur beschreibt, wenn sich der Wert minPrice und hour ändern?

                  1 Reply Last reply Reply Quote 0
                  • D
                    Diamand2k22 last edited by Diamand2k22

                    Hallo @paul53, kannst du mir nochmal helfen mit meiner Zeitfilterung.

                    die Funktion sieht aktuell so aus:

                    async function filterZeitVonXXToXX(arrZeit, zeit1, zeit2) {
                        const newArray = [];
                    
                        // console.warn(JSON.stringify(arrZeit));
                    
                        for (let i = 0; i < arrZeit.length; i++) {
                            const startTime = parseInt(arrZeit[i][1].split(':')[0]);
                            if(startTime <= zeit1.split(':')[0] || startTime >= zeit2.split(':')[0]) newArray.push(arrZeit[i]);
                            if (startTime == zeit1.split(':')[0]) {
                                break;    
                            }
                        }
                    
                        // console.warn(JSON.stringify(newArray));
                    
                        newArray.sort(function (a, b) {  // niedrieg preis sort
                            return b[0] - a[0];
                        });
                    
                        //console.warn(JSON.stringify(newArray));
                    
                        return newArray;    
                    }
                    

                    dabei ist arrZeit das Array mit den Zeiten und Preisen wie oben beschrieben.
                    zeit 1 würde z.B. 9:00 Uhr sein
                    zeit 2 ist die aktuelle Zeit z.B. 21:00Uhr

                    jetzt sollen als return nur die Zeiten zwischen 21:00 Uhr und 9:00 Uhr ausgegeben werden.
                    Bis 0:00 Uhr funktioniert das ganze, nach 0:00 schreibt er mir auch die Zeiten nach 9:00 Uhr ins Array.

                    Hast du ne idee?

                    paul53 1 Reply Last reply Reply Quote 0
                    • paul53
                      paul53 @Diamand2k22 last edited by

                      @diamand2k22 sagte: Zeiten nach 9:00 Uhr ins Array.

                      Versuche es mal mit Zahlen:

                              if(startTime <= parseInt(zeit1) || startTime >= parseInt(zeit2)) newArray.push(arrZeit[i]);
                      
                      D 1 Reply Last reply Reply Quote 0
                      • D
                        Diamand2k22 @paul53 last edited by Diamand2k22

                        @paul53

                        Hallo Paul,

                        habs mal getestet, aber immer noch selbes Problem.
                        hier mein Test Array

                            tibberPoiAll = [[0.3659,"18:00","18:30"],[0.3659,"18:30","19:00"],[0.3657,"17:00","17:30"],[0.3657,"17:30","18:00"],[0.3631,"19:00","19:30"],[0.3631,"19:30","20:00"],[0.3559,"16:00","16:30"],[0.3559,"16:30","17:00"],[0.3553,"20:00","20:30"],[0.3553,"20:30","21:00"],[0.3486,"22:00","22:30"],[0.3486,"22:30","23:00"],[0.3454,"21:00","21:30"],[0.3454,"21:30","22:00"],[0.3433,"08:00","08:30"],[0.3408,"23:00","23:30"],[0.3408,"23:30","00:00"],[0.3356,"07:00","07:30"],[0.3356,"07:30","08:00"],[0.3232,"00:00","00:30"],[0.3232,"00:30","01:00"],[0.3194,"06:00","06:30"],[0.3194,"06:30","07:00"],[0.3148,"05:00","05:30"],[0.3148,"05:30","06:00"],[0.3147,"01:00","01:30"],[0.3147,"01:30","02:00"],[0.3142,"02:00","02:30"],[0.3142,"02:30","03:00"],[0.3097,"04:00","04:30"],[0.3097,"04:30","05:00"],[0.3083,"03:00","03:30"],[0.3083,"03:30","04:00"]]
                        
                            tibberPoihigh = await filterZeitVonXXToXX(tibberPoiAll, '09:00', '01:00');
                        

                        rauskommen tut dabei

                        [ [ 0.3659, '18:00', '18:30' ], [ 0.3659, '18:30', '19:00' ], [ 0.3657, '17:00', '17:30' ], [ 0.3657, '17:30', '18:00' ], [ 0.3631, '19:00', '19:30' ], [ 0.3631, '19:30', '20:00' ], [ 0.3559, '16:00', '16:30' ], [ 0.3559, '16:30', '17:00' ], [ 0.3553, '20:00', '20:30' ], [ 0.3553, '20:30', '21:00' ], [ 0.3486, '22:00', '22:30' ], [ 0.3486, '22:30', '23:00' ], [ 0.3454, '21:00', '21:30' ], [ 0.3454, '21:30', '22:00' ], [ 0.3433, '08:00', '08:30' ], [ 0.3408, '23:00', '23:30' ], [ 0.3408, '23:30', '00:00' ], [ 0.3356, '07:00', '07:30' ], [ 0.3356, '07:30', '08:00' ], [ 0.3232, '00:00', '00:30' ], [ 0.3232, '00:30', '01:00' ], [ 0.3194, '06:00', '06:30' ], [ 0.3194, '06:30', '07:00' ], [ 0.3148, '05:00', '05:30' ], [ 0.3148, '05:30', '06:00' ], [ 0.3147, '01:00', '01:30' ], [ 0.3147, '01:30', '02:00' ], [ 0.3142, '02:00', '02:30' ], [ 0.3142, '02:30', '03:00' ], [ 0.3097, '04:00', '04:30' ], [ 0.3097, '04:30', '05:00' ], [ 0.3083, '03:00', '03:30' ], [ 0.3083, '03:30', '04:00' ] ]
                        

                        wenn ich die Zeit vor 0 Uhr rein nehme, wird richtig gefiltert, also in dem Fall von 21:00 Uhr bis 9:00 Uhr

                            tibberPoihigh = await filterZeitVonXXToXX(tibberPoiAll, '09:00', '21:00');
                        

                        Ausgabe:

                        [ [ 0.3486, '22:00', '22:30' ], [ 0.3486, '22:30', '23:00' ], [ 0.3454, '21:00', '21:30' ], [ 0.3454, '21:30', '22:00' ], [ 0.3433, '08:00', '08:30' ], [ 0.3408, '23:00', '23:30' ], [ 0.3408, '23:30', '00:00' ], [ 0.3356, '07:00', '07:30' ], [ 0.3356, '07:30', '08:00' ], [ 0.3232, '00:00', '00:30' ], [ 0.3232, '00:30', '01:00' ], [ 0.3194, '06:00', '06:30' ], [ 0.3194, '06:30', '07:00' ], [ 0.3148, '05:00', '05:30' ], [ 0.3148, '05:30', '06:00' ], [ 0.3147, '01:00', '01:30' ], [ 0.3147, '01:30', '02:00' ], [ 0.3142, '02:00', '02:30' ], [ 0.3142, '02:30', '03:00' ], [ 0.3097, '04:00', '04:30' ], [ 0.3097, '04:30', '05:00' ], [ 0.3083, '03:00', '03:30' ], [ 0.3083, '03:30', '04:00' ] ]
                        
                        paul53 1 Reply Last reply Reply Quote 0
                        • paul53
                          paul53 @Diamand2k22 last edited by

                          @diamand2k22 sagte: rauskommen tut dabei

                          Ja, alle Zeiten sind >= 1 (Uhr).
                          Was soll das Ergebnis sein?

                          D 1 Reply Last reply Reply Quote 0
                          • D
                            Diamand2k22 @paul53 last edited by Diamand2k22

                            @paul53

                            so sieht der Filter aus: tibberPoihigh = await filterZeitVonXXToXX(tibberPoiAll, _sunup, nowhour);

                            _sunup = Zeit Sonnenaufgang z.B. 9:00
                            nowhour ist die aktuelle Zeit z.B. 01:00

                            es sollen jetzt die Zeiten aus dem Array zwischen 1 Uhr und 9 Uhr ausgegeben werden, aber es werden auch die Zeiten nach 9 Uhr ausgegeben.

                            wie gesagt, wenn nowhour 23:00 ist, dann funktioniert der Filter bis 9:00 Uhr, wenn nowhour nach 0:00 Uhr dann werden auch Zeiten größer 9 Uhr ausgegeben

                            paul53 1 Reply Last reply Reply Quote 0
                            • paul53
                              paul53 @Diamand2k22 last edited by

                              @diamand2k22 sagte: Zeiten aus dem Array zwischen 1 Uhr und 9 Uhr

                              Versuche es mal so:

                              if(parseInt(zeit2) > parseInt(zeit1)) {
                                  if(startTime <= parseInt(zeit1) || startTime >= parseInt(zeit2)) newArray.push(arrZeit[i]);
                              } else {
                                  if(startTime <= parseInt(zeit1) && startTime >= parseInt(zeit2)) newArray.push(arrZeit[i]);
                              }
                              
                              D 1 Reply Last reply Reply Quote 0
                              • D
                                Diamand2k22 @paul53 last edited by Diamand2k22

                                @paul53

                                ich glaub das war es:

                                tibberPoihigh = await filterZeitVonXXToXX(tibberPoiAll, '09:00', '01:00');
                                
                                [ [ 0.3433, '08:00', '08:30' ], [ 0.3356, '07:00', '07:30' ], [ 0.3356, '07:30', '08:00' ], [ 0.3194, '06:00', '06:30' ], [ 0.3194, '06:30', '07:00' ], [ 0.3148, '05:00', '05:30' ], [ 0.3148, '05:30', '06:00' ], [ 0.3147, '01:00', '01:30' ], [ 0.3147, '01:30', '02:00' ], [ 0.3142, '02:00', '02:30' ], [ 0.3142, '02:30', '03:00' ], [ 0.3097, '04:00', '04:30' ], [ 0.3097, '04:30', '05:00' ], [ 0.3083, '03:00', '03:30' ], [ 0.3083, '03:30', '04:00' ] ]
                                
                                tibberPoihigh = await filterZeitVonXXToXX(tibberPoiAll, '09:00', '21:00');
                                
                                [ [ 0.3486, '22:00', '22:30' ], [ 0.3486, '22:30', '23:00' ], [ 0.3454, '21:00', '21:30' ], [ 0.3454, '21:30', '22:00' ], [ 0.3433, '08:00', '08:30' ], [ 0.3408, '23:00', '23:30' ], [ 0.3408, '23:30', '00:00' ], [ 0.3356, '07:00', '07:30' ], [ 0.3356, '07:30', '08:00' ], [ 0.3232, '00:00', '00:30' ], [ 0.3232, '00:30', '01:00' ], [ 0.3194, '06:00', '06:30' ], [ 0.3194, '06:30', '07:00' ], [ 0.3148, '05:00', '05:30' ], [ 0.3148, '05:30', '06:00' ], [ 0.3147, '01:00', '01:30' ], [ 0.3147, '01:30', '02:00' ], [ 0.3142, '02:00', '02:30' ], [ 0.3142, '02:30', '03:00' ], [ 0.3097, '04:00', '04:30' ], [ 0.3097, '04:30', '05:00' ], [ 0.3083, '03:00', '03:30' ], [ 0.3083, '03:30', '04:00' ] ]
                                

                                jetzt sieht's gut aus! vielen Dank für deine Hilfe!

                                1 Reply Last reply Reply Quote 0
                                • D
                                  Diamand2k22 @paul53 last edited by Diamand2k22

                                  @paul53 said in Zeiten filtern Array:

                                  @diamand2k22 sagte: die kleinste Uhrzeit

                                  Das funktioniert z.B. mit einem String-Vergleich.

                                  const arr = [ [ 0.2325, '23:00', '23:30' ], [ 0.2425, '03:00', '03:30' ], [ 0.2425, '03:30', '04:00' ], [ 0.2434, '02:00', '02:30' ], [ 0.2434, '02:30', '03:00' ], [ 0.2492, '04:00', '04:30' ] ];
                                  
                                  let first = '23:59';
                                  for(const ele of arr) {
                                      if(ele[1] < first) first = ele[1];
                                  };
                                  log(first);
                                  

                                  Hallo @paul53, ich habe hier selbiges Problem wie bei dem Skript davor, dass er Zeiten, die vor 00:00 Uhr sind, nicht berücksichtig. Die Variable first ist bei dem Array 00:00, müsste aber 23:00 sein. Kannst du mir hier weiterhelfen?

                                  Danke dir!

                                  1 Reply Last reply Reply Quote 0
                                  • D
                                    Diamand2k22 last edited by

                                    @paul53

                                    bräuchte nochmal deine Hilfe.

                                    folgendes Array:

                                    [ 0.3488, '03:00', '03:30' ],
                                    [ 0.3488, '03:30', '04:00' ],
                                    [ 0.3497, '02:00', '02:30' ],
                                    [ 0.3497, '02:30', '03:00' ]
                                    

                                    der code ist:

                                    let first = '23:59';
                                    for(const ele of arr) {
                                        if(ele[1] < first) first = ele[1];
                                    };
                                    

                                    er gibt als Uhrzeit 02:30 Uhr aus, normal sollte es doch 02:00 Uhr sein oder?

                                    paul53 1 Reply Last reply Reply Quote 0
                                    • paul53
                                      paul53 @Diamand2k22 last edited by paul53

                                      @diamand2k22 sagte; sollte es doch 02:00 Uhr sein oder?

                                      Ja, es sollte die erste Zeit genommen werden, denn sie ist kleiner.

                                      D 1 Reply Last reply Reply Quote 0
                                      • D
                                        Diamand2k22 @paul53 last edited by Diamand2k22

                                        @paul53

                                        habe gerade nochmal im Log geschaut,

                                        wenn das Array so aussieht:

                                          [ 0.3488, '03:00', '03:30' ],
                                          [ 0.3488, '03:30', '04:00' ],
                                          [ 0.3497, '02:00', '02:30' ]
                                        

                                        dann gibt er 02:00 aus

                                        wenn das Array aber so aussieht:

                                        [ 0.3488, '03:00', '03:30' ],
                                        [ 0.3488, '03:30', '04:00' ],
                                        [ 0.3497, '02:00', '02:30' ],
                                        [ 0.3497, '02:30', '03:00' ]
                                        
                                        

                                        dann gibt er 02:30 Uhr aus.

                                        anscheinend gibt es ein Problem wenn 2x 02:30 Uhr im Array steht

                                        T 1 Reply Last reply Reply Quote 0
                                        • T
                                          ticaki Developer @Diamand2k22 last edited by ticaki

                                          @diamand2k22
                                          Bildschirmfoto 2025-01-31 um 23.15.31.png

                                          const arr = [[ 0.3488, '03:00', '03:30' ],
                                          [ 0.3488, '03:30', '04:00' ],
                                          [ 0.3497, '02:00', '02:30' ],
                                          [ 0.3497, '02:30', '03:00' ]]
                                          
                                          let first = '23:59';
                                          for(const ele of arr) {
                                              //@ts-ignore
                                              if(ele[1] < first) first = ele[1];
                                          };
                                          log(first)
                                          

                                          ignoriere das ts-ignore 🙂

                                          javascript.0	23:15:11.732	info	02:00
                                          
                                          D 1 Reply Last reply Reply Quote 0
                                          • D
                                            Diamand2k22 @ticaki last edited by Diamand2k22

                                            @ticaki @paul53

                                            hab den Fehler bei mir im Code gefunden, danke euch für eure Hilfe!

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            389
                                            Online

                                            31.8k
                                            Users

                                            80.0k
                                            Topics

                                            1.3m
                                            Posts

                                            4
                                            53
                                            2261
                                            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