Skip to content
  • Home
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Mehrdimensionale Arrays

NEWS

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    15
    1
    215

  • Jahresrückblick 2025 – unser neuer Blogbeitrag ist online! ✨
    BluefoxB
    Bluefox
    17
    1
    4.4k

  • Neuer Blogbeitrag: Monatsrückblick - Dezember 2025 🎄
    BluefoxB
    Bluefox
    13
    1
    1.3k

Mehrdimensionale Arrays

Scheduled Pinned Locked Moved JavaScript
javascript
13 Posts 5 Posters 2.6k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Georgius
    wrote on last edited by
    #1

    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
    0
    • carsten04C Offline
      carsten04C Offline
      carsten04
      Developer
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1
      • PeoplesP Offline
        PeoplesP Offline
        Peoples
        wrote on last edited by Peoples
        #3

        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

        Ich beantworte keine Fragen zu Themen via PN

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Georgius
          wrote on last edited by
          #4

          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.

          PeoplesP AsgothianA 2 Replies Last reply
          0
          • G Georgius

            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.

            PeoplesP Offline
            PeoplesP Offline
            Peoples
            wrote on last edited by Peoples
            #5

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

            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

            Ich beantworte keine Fragen zu Themen via PN

            1 Reply Last reply
            0
            • G Georgius

              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.

              AsgothianA Offline
              AsgothianA Offline
              Asgothian
              Developer
              wrote on last edited by Asgothian
              #6

              @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.

              ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
              "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

              paul53P 1 Reply Last reply
              0
              • AsgothianA 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.

                paul53P Offline
                paul53P Offline
                paul53
                wrote on last edited by
                #7

                @Asgothian typeof ist keine Funktion.

                Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
                Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

                AsgothianA 1 Reply Last reply
                0
                • paul53P paul53

                  @Asgothian typeof ist keine Funktion.

                  AsgothianA Offline
                  AsgothianA Offline
                  Asgothian
                  Developer
                  wrote on last edited by Asgothian
                  #8

                  @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)

                  ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
                  "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

                  paul53P 1 Reply Last reply
                  1
                  • AsgothianA 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)

                    paul53P Offline
                    paul53P Offline
                    paul53
                    wrote on last edited by
                    #9

                    @Asgothian sagte:

                    It supports two forms of syntax:

                    Danke, das wusste ich noch nicht.

                    Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
                    Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Georgius
                      wrote on last edited by Georgius
                      #10

                      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
                      0
                      • G Offline
                        G Offline
                        Georgius
                        wrote on last edited by Georgius
                        #11

                        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

                        AsgothianA 1 Reply Last reply
                        0
                        • G 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

                          AsgothianA Offline
                          AsgothianA Offline
                          Asgothian
                          Developer
                          wrote on last edited by Asgothian
                          #12

                          @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.

                          ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
                          "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Georgius
                            wrote on last edited by
                            #13

                            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
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            Support us

                            ioBroker
                            Community Adapters
                            Donate

                            576

                            Online

                            32.7k

                            Users

                            82.4k

                            Topics

                            1.3m

                            Posts
                            Community
                            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
                            ioBroker Community 2014-2025
                            logo
                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Home
                            • Recent
                            • Tags
                            • Unread 0
                            • Categories
                            • Unreplied
                            • Popular
                            • GitHub
                            • Docu
                            • Hilfe