Navigation

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

    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

    findindex

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

      Moin,

      ich habe folgendes Array (Beispiel)

      const sensors = {
          "devices": [
              {
                  "name": "tuer123",
                  "code": "123"
              },
              {
                  "name": "tuer456",
                  "code": "456"
              },
              {
                  "name": "tuer789",
                  "code": "789"
              }        
          ]};
      

      Daraus möchte ich jetzt den Index des Eintrages ermitteln, der einen bestimmten suchcode hat.
      Also in etwa so (was aber nicht funktioniert)

      var suchcode = "456";
      var index = sensors.devices.code.findIndex(suchcode);
      

      Wie mache ich es richtig?
      Geht das überhaupt?

      Danke im Voraus

      blauholsten paul53 AlCalzone 3 Replies Last reply Reply Quote 0
      • blauholsten
        blauholsten Developer @tcj last edited by

        @tcj hi,

        indexOf oder includes sollte deine Wünsche entsprechen

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

          var objArray = [
          { id: 0, name: 'Object 0', otherProp: '321' },
          { id: 1, name: 'O1', otherProp: '648' },
          { id: 2, name: 'Another Object', otherProp: '850' },
          { id: 3, name: 'Almost There', otherProp: '046' },
          { id: 4, name: 'Last Obj', otherProp: '984' }
          ];

          klassich:
          function findObjectByKey(array, key, value) {
          for (var i = 0; i < array.length; i++) {
          if (array[i][key] === value) {
          return array[i];
          }
          }
          return null;
          }
          var obj = findObjectByKey(objArray, 'id', 3);

          ES6
          let obj = objArray.find(obj => obj.id == 3);

          ob es6 geht musst du ausprobieren bzw. hier mit deiner version abgleichen
          https://nodejs.org/en/docs/es6/

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

            @tcj sagte:

            Wie mache ich es richtig?

            Z.B. so

            const arr = sensors.devices;
            var suchcode = "456";
            var index = null;
            for(let i = 0; i < arr.length; i++) {
                if(arr[i].code == suchcode) {
                    index = i;
                    break;
                }
            }
            
            1 Reply Last reply Reply Quote 0
            • AlCalzone
              AlCalzone Developer @tcj last edited by AlCalzone

              @tcj sagte in findindex:

              var index = sensors.devices.code.findIndex(suchcode);

              War schon fast richtig. Lies dir nochmal die Doku zu findIndex durch:
              https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

              Die Methode findIndex() gibt den Index des ersten Elements im Array zurück, das die bereitgestellte Testfunktion erfüllt.

              Du übergibst also nicht den gesuchten Code (woher soll das Programm wissen, wo sich dieser im Objekt befindet), sondern eine Funktion, die jeden Eintrag testet, ob er der gesuchte ist.

              var index = sensors.devices.findIndex(obj => obj.code === suchcode);
              

              wäre richtig.

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

                Danke an alle für eure Hinweise.
                Ich habe jetzt ein Funktion geschrieben, die das Array durchläuft.
                In etwa so wie von @OliverW gezeigt.

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

                Support us

                ioBroker
                Community Adapters
                Donate

                936
                Online

                31.9k
                Users

                80.2k
                Topics

                1.3m
                Posts

                javascript
                5
                6
                379
                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