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

    • 15. 05. Wartungsarbeiten am ioBroker Forum

    • Monatsrückblick - April 2025

    • Minor js-controller 7.0.7 Update in latest repo

    Zeiten filtern Array

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

      @paul53

      Hallo Paul,

      hab folgendes Array, vorgefiltert mit z.B. Zeiten ab 0:00

       [ [ 0.3839, '17:00', '17:30' ], [ 0.3839, '17:30', '18:00' ], [ 0.3766, '18:00', '18:30' ], [ 0.3766, '18:30', '19:00' ] ]
      

      Die Variable sunup = 8:30 Uhr

      async function filterZeit(arrZeit, sunup) {
          const newArray = [];
      
          for (let i = 0; i < arrZeit.length; i++) {
              const startTime = parseInt(arrZeit[i][1].split(':')[0]);
              newArray.push(arrZeit[i]);
              if (startTime == sunup.split(':')[0]) {
                  break;    
              }
          }
      
          newArray.sort(function (a, b) {
              return b[0] - a[0];
          });
      
          return newArray;
      }
      

      jetzt soll das Skript die Zeiten zwischen 0:00 Uhr und 08:30 Uhr aus dem Array ausgeben.
      Wenn jetzt Zeiten zwischen 0:00 Uhr und 8:30 Uhr im Array sind, werden die auch ausgegeben, aber wenn keine Zeiten zwischen 00:00 Uhr und 8:30 Uhr enthalten sind, wir aber trotzdem das Array mit Zeiten größer > 8:30 Uhr ausgeben

      wie bekomm ich es hin, dass dann ein leeres array [] ausgegeben wird?

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

        @diamand2k22 sagte: leeres array [] ausgegeben wird?

        Zeile 6:

                if(startTime <= sunup.split(':')[0]) newArray.push(arrZeit[i]);
        
        D 1 Reply Last reply Reply Quote 0
        • D
          Diamand2k22 @paul53 last edited by

          @paul53

          funktioniert, vielen Dank!

          Grüße

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

            Hallo @paul53

            hab wieder eine Frage.

            ich habe das Array

            [ [ 0.3839, '17:00', '17:30' ], [ 0.3839, '17:30', '18:00' ], [ 0.3766, '18:00', '18:30' ], [ 0.3766, '18:30', '19:00' ] ]
            
            

            jetzt hab ich ein anderes Array mit Zeiten aber ohne Preise

            [ [ '17:00', '17:30' ], [ '17:30', '18:00' ], [ '20:00', '20:30' ], [ '21:30', '22:00' ] ]
            

            wie ist es möglich, die unteren Zeiten aus dem oberen Array rauszuschneiden?
            Es kann auch vorkommen, dass z.B. Zeiten aus dem unteren Array in dem oberen nicht enthalten sind.

            Danke und Grüße

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

              @diamand2k22

              in c ist dann das gefilterte a-array
              das sind mehr oder weniger 2 schleifen.
              der filter befehl klappert alle elemente von a der reihe nach ab.
              bei jedem durchgang wird dann die variable el gefüllt.
              mit el wird dann das b-array durchsucht und die einzelnen inhalte verglichen.
              auch beim suchen wird immer wieder jedes einzelne element in die variable sel gespeichert.
              bei erfolg darf das element bleiben, bei misserfolg wird es ausgefiltert

              let a=[ [ 0.3839, '17:00', '17:30' ], [ 0.3839, '17:30', '18:00' ], [ 0.3766, '18:00', '18:30' ], [ 0.3766, '18:30', '19:00' ] ];
              let b=[ [ '17:00', '17:30' ], [ '17:30', '18:00' ], [ '20:00', '20:30' ], [ '21:30', '22:00' ] ];
              let c= a.filter(ael=>b.find(bel=>ael[1]==bel[0]&&ael[2]==bel[1]));
              

              hier mal nochmal eine übersichtlichere darstellung gegenüber der kompakten schreibweise

              let c = a.filter(
              	ael => b.find(
              		bel => ael[1] == bel[0] && ael[2] == bel[1]
              	)
              );
              
              

              https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
              https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

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

                @oliverio
                @paul53

                super danke!

                aber jetzt gibt er mir im array c die Zeiten aus, die in Array b stehen und in array a vorkommen, aber er soll ja die zeiten aus array b von array a rausschmeißen und dann in array c die zeiten von array a ohne die zeiten aus array b ausgeben.

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

                  @diamand2k22

                  mal schauen ob du den unterschied findest 🙂

                  let a=[ [ 0.3839, '17:00', '17:30' ], [ 0.3839, '17:30', '18:00' ], [ 0.3766, '18:00', '18:30' ], [ 0.3766, '18:30', '19:00' ] ];
                  let b=[ [ '17:00', '17:30' ], [ '17:30', '18:00' ], [ '20:00', '20:30' ], [ '21:30', '22:00' ] ];
                  let c= a.filter(ael=>!b.find(bel=>ael[1]==bel[0]&&ael[2]==bel[1]));
                  
                  

                  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT

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

                    @oliverio

                    die Negierung 😅
                    danke es funktioniert nun!

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

                      @paul53
                      @OliverIO

                      Hallo ihr zwei, ich hab nochmal eine Frage zum Filtern von Zeiten.

                      Wie filter ich am einfachsten aus folgendem Array die kleinste Uhrzeit, also in dem Fall wäre es 2:00 Uhr, der Preis ist in dem Fall egal. Die Länge des Arrays kann beliebig variieren.

                      [ [ 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' ] ]
                      

                      Danke und Grüße

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

                        @diamand2k22 sagte in Zeiten filtern Array:

                        nur wenn mindestens ein element im array drin ist., ansonsten muss man das ergebnis vor ausgabe nochmal prüfen ob array auch mindestens ein element enthält

                        funktionsweise:
                        array wird sortiert anhand des 2 wertes jedes elements. damit daraus eine zahl wird, wird der : entfernt und in eine zahl umgewandelt.
                        am ende wird das erste element entnommen, was dann das kleinste ist

                        let 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' ] ]
                        console.log(arr.sort((a,b)=>parseInt(a[1].replace(":"))-parseInt(b[1].replace(":")))[0])
                        
                        1 Reply Last reply Reply Quote 0
                        • paul53
                          paul53 @Diamand2k22 last edited by paul53

                          @diamand2k22 sagte: die kleinste Uhrzeit

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

                          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' ] ];
                          
                          let first = '23:59';
                          for(const ele of arr) {
                              if(ele[1] < first) first = ele[1];
                          };
                          log(first);
                          
                          D 2 Replies Last reply Reply Quote 0
                          • D
                            Diamand2k22 @paul53 last edited by

                            @OliverIO
                            @paul53

                            Vielen Dank, ich hab beide Varianten getestet und es funktioniert!
                            Schönen Sonntag Abend euch noch!

                            Grüße

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

                              @diamand2k22 said in Zeiten filtern Array:

                              @paul53

                              Hallo Paul,

                              hab folgendes Array, vorgefiltert mit z.B. Zeiten ab 0:00

                               [ [ 0.3839, '17:00', '17:30' ], [ 0.3839, '17:30', '18:00' ], [ 0.3766, '18:00', '18:30' ], [ 0.3766, '18:30', '19:00' ] ]
                              

                              Die Variable sunup = 8:30 Uhr

                              async function filterZeitToSunup(arrZeit, sunup) {
                                 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 <= sunup.split(':')[0]) newArray.push(arrZeit[i]);
                                     if (startTime == sunup.split(':')[0]) { // || startTime == 14
                                         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;
                              }
                              

                              das Skript soll die Zeiten zwischen 0:00 Uhr und 08:30 Uhr aus dem Array ausgeben.

                              Wenn jetzt Zeiten zwischen 0:00 Uhr und 8:30 Uhr im Array sind, werden die auch ausgegeben, aber wenn keine Zeiten zwischen 00:00 Uhr und 8:30 Uhr enthalten sind, wir aber trotzdem das Array mit Zeiten größer > 8:30 Uhr ausgeben

                              wie bekomm ich es hin, dass dann ein leeres array [] ausgegeben wird?

                              @paul53

                              hab jetzt noch eine Variable sundown, die gibt den Zeitpunkt des Sonnenuntergangs aus z.B 16:19 Uhr.

                              aktuell werden in der Funktion nur Zeiten von 0:00 Uhr bis sunup berücksichtigt. wie muss ich die Funktion anpassen, dass nur Zeiten zwischen sundown und sunup ausgegeben werden?

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

                                @diamand2k22

                                mache es doch so wie paul es schon mal vorgeschlagen hat.

                                for (const a of arrZeit ) {
                                    if (a[1] > sunup && a[1] < sundown) {
                                        newArr.push(a)
                                    }
                                    // oder für kleiner sunup
                                    if (a[1] < sunup)) {
                                        newArr.push(a)
                                    }
                                }
                                

                                EDIT: bin mit dem [1] bissle durcheinander gekommen

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

                                  @ticaki

                                  da ist eben das Problem, dass er die Zeiten vor 0 Uhr nicht berücksichtig, da 22 Uhr > 8 Uhr als Beispiel

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

                                    @diamand2k22

                                    if (a[1] < sunup || a[1] > sundown) {
                                            newArr.push(a)
                                        }
                                    

                                    Das ist doch nur ein umstellen der vergleichszeichen?

                                    edit: und dann oder und nicht und 🙂

                                    @paul53
                                    Danke dafür! Ich hab da immer aufwendig mit date rumgemacht, deines ist deutlich lesbarer

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

                                      @ticaki

                                      ok danke, d.h. dann auf die Funktion bezogen:

                                      async function filterZeitSunup(arrZeit, sunup, sundown) {
                                          const newArray = [];
                                      
                                          // console.warn(JSON.stringify(arrZeit));
                                      
                                          for (let i = 0; i < arrZeit.length; i++) {
                                              const startTime = parseInt(arrZeit[i][1].split(':')[0]);
                                              newArray.push(arrZeit[i]);
                                              if (arrZeit[1] < sunup || arrZeit[1] > sundown) {
                                                  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;
                                      }
                                      
                                      T 1 Reply Last reply Reply Quote 0
                                      • T
                                        ticaki Developer @Diamand2k22 last edited by ticaki

                                        @diamand2k22
                                        nein

                                        Das wird nicht gehen, da ich nicht den ganzen Topic gelesen habe und nicht genaus weiß was für daten da im arrZeit sind, kann ich gerade nix dazu sagen, bin auch zu müde um es jetzt zu verstehen 🙂

                                        D 1 Reply Last reply Reply Quote 0
                                        • 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
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            521
                                            Online

                                            31.6k
                                            Users

                                            79.4k
                                            Topics

                                            1.3m
                                            Posts

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