Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Vergleich von Objekten in 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

    Vergleich von Objekten in Arrays

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

      @schmakus
      Wo ist in diesem Array oder den Objekten dein eindeutiger Schlüssel?

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

        @schmakus

        Entscheidend ist die Frage was du tun willst wenn doch eine Zahl an mehreren Stellen vorkommt.

        let present = '|';
        for (const obj of arr) {
          for (const id of arr.lights {
            if (present.indexOf(`|${id}|`) > -1) {
               //duplikat gefunden
            } else {
              present = present.concat(`${id}|`);
            }
          }
        }
        
        Schmakus 1 Reply Last reply Reply Quote 0
        • Schmakus
          Schmakus Developer @Asgothian last edited by

          @asgothian
          Ich möchte eine Ausgabe im Log, wenn eine Zahl mehrmals vorkommt.
          Werde deinen Code gleich mal testen!

          Habe zwischenzeitlich dieses aufwändige Teil zusammengebastelt.

          function find_duplicate_in_array(arrOne) {
          	//Alle Werte aus allen Lights mappen
          	const arr = arrOne.map(a => a.lights);
          	//Flaches Array erstellen
            const array = arr.flat()
          
            const object = {};
            const result = [];
          
            array.forEach(item => {
              if(!object[item])
                object[item] = 0;
              object[item] += 1;
            })
          
            for (const prop in object) {
              if(object[prop] >= 2) {
                result.push(prop);
              }
            }
          
            return result;
          }
          
          //Funktion ausführen
          let dups = find_duplicate_in_array(arrOne);
          //Inhalt zählen
          let count = dups.length;
          
          if (count > 0) {
          	if (count > 1) {
              dups = dups.join(' und ');
              console.warn(`Lampen ${dups} kommen mehrmals vor!`);
             } else {
             	console.warn(`Lampe ${dups} kommt mehrmals vor!`);
             }
          } else {
          	console.log(`Keine Lampen kommen mehrmals vor. Gut!`);
          }
          
          1 Reply Last reply Reply Quote 0
          • OliverIO
            OliverIO @Schmakus last edited by

            @schmakus sagte in Vergleich von Objekten in Arrays:

            arr = [ { lights: [1,2] }, { lights: [3,4] }, { lights: [1,5] } ];

            Die beste Vorgehensweise ist, bereits vor dem hinzufügen zu prüfen, ob der wert (nicht key) bereits vorhanden ist.

            arr.reduce((acc,el)=>el.lights.includes(1)) // ergibt true
            arr.reduce((acc,el)=>el.lights.includes(6)) // ergibt false
            
            Schmakus 1 Reply Last reply Reply Quote 0
            • Schmakus
              Schmakus Developer @arteck last edited by

              @arteck Ein Replace nicht. Ich möchte nur verhindern, bzw. eine Ausgabe loggen, wenn eine Lampe in mehreren Gruppen zugeordnet wurde.

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

                @oliverio Die Werte werden aktuell noch manuell eingetragen, bzw. die Objekte manuell erstellt. Deshalb die Abfrage beim Starten eines Scripts.

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

                  @schmakus sagte in Vergleich von Objekten in Arrays:

                  @oliverio Die Werte werden aktuell noch manuell eingetragen, bzw. die Objekte manuell erstellt. Deshalb die Abfrage beim Starten eines Scripts.

                  ok, falls schon duplikate drin sind dann so

                  var data=[];arr.map(i1=>i1.lights.map(i2=>isNaN(data[i2])?data[i2]=1:data[i2]++))
                  

                  data enthält dann eine Auflistung, welcher wert wie oft enthalten ist.

                  "[null,2,1,1,1,1]"

                  das bedeutet:
                  key 0 exisitiert gar nicht, daher null
                  key 1 exisitiert 2 mal
                  key 2 exisitiert 1 mal
                  key 3 exisitiert 1 mal
                  key 4 exisitiert 1 mal
                  key 5 exisitiert 1 mal
                  key 6 exisitiert 1 mal

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

                    @schmakus sagte in Vergleich von Objekten in Arrays:

                    Ausgabe loggen, wenn eine Lampe in mehreren Gruppen zugeordnet wurde.

                    Vorschlag:

                    function findDuplicate(arr) {
                        let found = [];
                        for(let i = 0; i < arr.length; i++) {
                            let lights = arr[i].lights;
                            for(let j = 0; j < lights.length; j++) {
                                if(found.includes(lights[j])) log('Lampe ' + lights[j] + ' kommt mehrmals vor!', 'warn');
                                else found.push(lights[j]);
                            }
                        }
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • OliverIO
                      OliverIO last edited by OliverIO

                      @paul53 sagte in Vergleich von Objekten in Arrays:

                      @schmakus sagte in Vergleich von Objekten in Arrays:

                      Ausgabe loggen, wenn eine Lampe in mehreren Gruppen zugeordnet wurde.

                      Vorschlag:

                      function findDuplicate(arr) {
                          let found = [];
                          for(let i = 0; i < arr.length; i++) {
                              let lights = arr[i].lights;
                              for(let j = 0; j < lights.length; j++) {
                                  if(found.includes(lights[j])) log('Lampe ' + lights[j] + ' kommt mehrmals vor!', 'warn');
                                  else found.push(lights[j]);
                              }
                          }
                      }
                      

                      das folgende meldet, sobald eine lampe das 2. mal oder noch öfters vorkommt, jedes mit einer einzelnen meldung

                      var data=[];arr.map(i1=>i1.lights.map(i2=>{isNaN(data[i2])?data[i2]=1:data[i2]++;data[i2]>1?console.log("found duplicate "+i2):""}))
                      

                      das hier gibt Meldungen aus, wenn alles eingesammelt wurde

                      var data=[];
                      arr.map(i1=>i1.lights.map(i2=>{isNaN(data[i2])?data[i2]=1:data[i2]++}));
                      data.map((el,i)=>el>1?console.log("Lampe "+i+" kommt "+el+" vor"):"")
                      
                      Schmakus 1 Reply Last reply Reply Quote 0
                      • Schmakus
                        Schmakus Developer @OliverIO last edited by

                        Vielen Dank euch allen! Viele Wege führen nach Rom.
                        Aber das von @OliverIO gefällt mir sehr gut. Schön kompaktes Coding.

                        @paul53, deine Schleifen sind gut lesbar. Danke.

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

                        Support us

                        ioBroker
                        Community Adapters
                        Donate

                        910
                        Online

                        31.8k
                        Users

                        80.0k
                        Topics

                        1.3m
                        Posts

                        5
                        12
                        409
                        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