Navigation

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

    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

    Mehrdimensionale Arrays

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

      Irgendwie verzweifel ich.

      Wie initialisiere ich ein 2-dimensionales Array?

      Gefüllt soll es dann in einer Schleife werden.

      Hab gesucht, aber nichts gefunden.

      Nehme auch gerne Links zu Anleitungen bzw. Buchtipps entgegen.

      Danke

      1 Reply Last reply Reply Quote 0
      • carsten04
        carsten04 Developer last edited by

        Schau hier mal: The Modern Javascript Tutorial
        Aus meiner Sicht eine der Besten online-Seiten zum Thema javascript.

        1 Reply Last reply Reply Quote 1
        • Peoples
          Peoples last edited by Peoples

          Anlegen kannst du so zum Beispiel:

          var Array = [];       
                                           
               Array[0] = [];
               Array[0][0] = 'Text1';
               Array[0][1] = 'Text2';
               Array[0][2] = 'Text3';   
          
          
          Array[1] = [];
               Array[1][0] = 'Text4';
               Array[1][1] = 'Text5';
               Array[1][2] = 'Text6';
          
          
          // Und ausgeben so:
          
          log(Array[0][0]); // Text1
          log(Array[1][2]); // Text6
          

          Bei der Verwendung von Schleifen kannst du einen Zähler nehmen den du pro Umlauf hochzählst. Diesen Zähler verwendest du für den Array Index und schon kannst du via Schleife befüllen/ändern

          1 Reply Last reply Reply Quote 0
          • G
            Georgius last edited by

            Danke, diese Darstellung hab ich schon oft gefunde. Aber irgendwie hilft sie mir nicht weiter. Stehe wohl auf der Leitung.

            Der ganze Teil mit Array [0][0] = Text gehört in eine Schleife. Ist auch kein Problem.

            Was gehört jetzt in die Definiton?

            Var Arrayname = []
            For a ....
            Arrayname [a]= []
            For b ...
            Arraymame [a][b] = was auch immer
            }
            }

            Ich weiß, ist kein wirkliches JS.

            Peoples Asgothian 2 Replies Last reply Reply Quote 0
            • Peoples
              Peoples @Georgius last edited by Peoples

              @Georgius
              Gib mir doch Mal mehr Infos was du versuchst umzusetzen. Dann kann man da ja Mal versuchen was basteln 😉

              Btw. Vielleicht hilft dir auch das ein wenig:
              Link Text

              Das war das Backup Script bevor der Adapter gebaut wurde und da habe ich das genutzt

              1 Reply Last reply Reply Quote 0
              • Asgothian
                Asgothian Developer @Georgius last edited by Asgothian

                @Georgius sagte in Mehrdimensionale Arrays:

                Danke, diese Darstellung hab ich schon oft gefunde. Aber irgendwie hilft sie mir nicht weiter. Stehe wohl auf der Leitung.

                Der ganze Teil mit Array [0][0] = Text gehört in eine Schleife. Ist auch kein Problem.

                Was gehört jetzt in die Definiton?

                Das was du geschrieben hast war soweit gar nicht so schlecht. Sieht man besser mit code tags :

                var Arrayname = []
                for (let a =0;a<firstdimension; 1) {
                  Arrayname [a]= []
                  for (let b=0;b<seconddimension; 1) {
                    Arraymame [a][b] = was auch immer
                  }
                }
                

                Damit ist Arrayname als Array definiert. Das es ein mehrdimensionales Array ist wird in der Deklaration nicht hinterlegt. Bei JS sind arrays aber in der Grösse nicht definiert.. Du kannst also folgendes machen:

                function setarray(myarray,i,j, value) {
                  if (typeof(myarray) != "object")  { 
                    array = [] 
                  }
                  if (typeof(myarray[i]) != "object")  { 
                     myarray[i] = []; 
                  }
                  myarray[i][j] = value;
                  return myarray
                }
                

                A.

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

                  @Asgothian typeof ist keine Funktion.

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

                    @paul53 sagte in Mehrdimensionale Arrays:

                    @Asgothian typeof ist keine Funktion.

                    Siehe Hier:

                    The typeof operator returns the type of the argument. It’s useful when we want to process values of different types differently or just want to do a quick check.

                    It supports two forms of syntax:

                    As an operator: typeof x.
                    As a function: typeof(x).

                    (von javascript.info/types)

                    paul53 1 Reply Last reply Reply Quote 1
                    • paul53
                      paul53 @Asgothian last edited by

                      @Asgothian sagte:

                      It supports two forms of syntax:

                      Danke, das wusste ich noch nicht.

                      1 Reply Last reply Reply Quote 0
                      • G
                        Georgius last edited by Georgius

                        Wie gesagt, bin absoluter Anfänger in JS. )Amiga Basic hab ich einigermaßen gekonnt). Wie ist der Aufruf für die Funktion von Asgothian?
                        Konkret, wie muß "myarrray" übergeben werden?

                        1 Reply Last reply Reply Quote 0
                        • G
                          Georgius last edited by Georgius

                          Irgendwie komm ich nicht weiter.

                          Mein Programm bisher

                          
                          var tankstelle;
                          const id=0;
                          const namen=1;
                          const preis=2;
                          const adresse1=3;
                          const adresse2=4;
                          const dist=5;
                          const lon = 6;
                          const lat = 7;
                          
                          if (typeof(tankstelle) != "object")  { 
                              tankstelle = [] 
                            }
                            if (typeof(tankstelle[1]) != "object")  { 
                               tankstelle[1] = []; 
                            }
                          
                          on({id: 'javascript.0.Sprit_t.Sprit_JSON'/*Sprit JSON*/, change: 'any'}, function(obj)
                           {
                              
                              var index = 0;
                              var gasStation = JSON.parse(obj.state.val); 
                           var start =  "javascript.0.Sprit.Tankstelle_";
                              
                             for (index = 0; index < gasStation.length; ++index) {
                                  tankstelle[index][id]= (gasStation[index].id);
                                  tankstelle[index][namen]= (gasStation[index].name);
                                  tankstelle[index][preis]= (gasStation[index].prices[0].amount).toString().replace(".",",");
                                  tankstelle[index][adresse1]= (gasStation[index].location.address);
                                  tankstelle[index][adresse2]= (gasStation[index].location.postalCode + " "+ gasStation[index].location.city);
                                  tankstelle[index][dist]= (gasStation[index].distance);
                                  tankstelle[index][lon]= (gasStation[index].longitude);
                                  tankstelle[index][lat]= (gasStation[index].latitude);
                          
                              };
                             
                          
                          
                           });
                          

                          Ich bekomme beim durchlaufen den Fehler

                          00:16:36.972	error	javascript.0 at Object.<anonymous> (script.js.common.Sonstiges.Tanken:32:30)
                          

                          30 ist das "=" in

                           tankstelle[index][id]= (gasStation[index].id);
                          

                          Edit: einen Fehler gefunden und hier ausgebessert. Hauptproblem besteht weiter

                          Asgothian 1 Reply Last reply Reply Quote 0
                          • Asgothian
                            Asgothian Developer @Georgius last edited by Asgothian

                            @Georgius
                            Der Fehler ist, das du nur die erste Zeile des Arrays initialisierst.
                            (Zeile 16: Tankstelle[1] = [];)
                            Schon wenn index 2 wird versuchst du in ein nicht initialisiertes Objekt einen Wert einzutragen.

                            Ich würde in Zeile 27 ein
                            tankstelle[index] = [];
                            einfügen.

                            Was noch bleibt ist die Frage ob das JSON immer die gleiche Anzahl Tankstellen beinhaltet. Wenn ja ist alles gut. Wenn nein, dann kann es sein das alte Daten am Ende der Tabelle erhalten bleiben.

                            1 Reply Last reply Reply Quote 0
                            • G
                              Georgius last edited by

                              Danke, jetzt funktioniert es. Das Arraymodell ist irgendwie anders als ich sie kenne.

                              Auch danke für den Hinweis mit der Arraygröße..

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

                              Support us

                              ioBroker
                              Community Adapters
                              Donate

                              787
                              Online

                              31.8k
                              Users

                              80.0k
                              Topics

                              1.3m
                              Posts

                              javascript
                              5
                              13
                              2027
                              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