MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Help choosing more than one item from an array

    Scheduled Pinned Locked Moved Troubleshooting
    18 Posts 4 Posters 5.3k Views 4 Watching
    Loading More Posts
    • 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.
    • Mykle1M Offline
      Mykle1 Project Sponsor Module Developer
      last edited by

      I have this array in my MMM-EOL module

      lifeFormArray: {
                  "Tigers": "328674",
                  "Hummingbirds": "8021",
                  "Lions": "328672",
                  "Jaguar": "328606",
                  "Leopard": "328673",
                  "Cheetah": "328680",
                  "Fox": "19076",
                  "Deaths-head Moth": "50688",
                  "Great White Shark": "213726",
                  "Seals": "7666",
                  "Mosquitos": "473",
                  "Venus Flytrap": "71355",
                  "Cardinals": "19590",
                  "Humpback Whale": "328575",
                  "Praying Mantis": "487055",
                  "Dragonfly": "42274802",
                  "Tarantulas": "170",
                  "Bats": "7631",
                  "Bears": "7631",
                  "Cicadas": "2645413",
                  "Striped Bass": "211032",
                  "Alligators": "796029",
                  "Crocodiles": "1739",
                  "Snakes": "2815988",
                  "Cats": "7674",
                  "Birds": "695",
                  "Eagles": "8016",
                  "Bald Eagle": "1049121",
                  "Cactus": "4228",
                  "Anemones and Corals": "1746",
                  "Crabs, Lobsters, and Shrimps": "1183",
                  "Sequoia": "42665",
                  "Sea Horse": "218966",
      	    "Hominidae": "1653",
      	    "Primates": "1645",
                  "Otters": "328044",
              }
      

      In the config option you would enter, for instance:
      lifeForm: "Cats",

      This enters the id number for “Cats” (from the array) into the url like this:
      + this.config.lifeFormArray[this.config.lifeForm] +

      How can I modify this so that I can enter more than one lifeForm in the config, for instance, lifeForm: "Cats,Bats,Bears", so that the module enters the three id numbers from the array into the url, comma seperated with no spaces? I hope I have explained myself clearly. The url does work manually in a browser when using several id numbers, comma seperated, no spaces. I just don’t know how to call more than one item from the array.

      Many thanks ahead of time.

      Create a working config
      How to add modules

      strawberry 3.141S 1 Reply Last reply Reply Quote 1
      • strawberry 3.141S Offline
        strawberry 3.141 Project Sponsor Module Developer @Mykle1
        last edited by

        @mykle1 what you describe above is actually not an array, it’s an object (hash). Arrays are always defined with [] not {}.

        Now to your problem. you can create an array from your config value and save it in a variable e.g. var lifeForms = this.config.lifeForm.split(',');. it would look like this ['Cats', 'Bats', 'Bears']. Then you can loop over it to get the ids and save it into another variable

        var lifeFormIds =[];
        for (let lifeForm of lifeForms) {
          if (this.config.lifeFormArray[lifeForm]) {
            lifeFormIds.push(this.config.lifeFormArray[lifeForm]);
          }
        }
        

        To create the id list you just join it comma seoerated like lifeFormIds.join(',');

        Please create a github issue if you need help, so I can keep track

        justjim1220J Mykle1M 5 Replies Last reply Reply Quote 1
        • justjim1220J Offline
          justjim1220 Module Developer @strawberry 3.141
          last edited by

          @strawberry-3-141

          just to be sure you understand…

          He’s not looking for it to show all the lifeforms at the same time, but different ones in intervals set by the user.

          Like with Cats, Bats, Bears… Say at an hourly interval show each one individually.

          I’m looking at this as well, I brought up the idea with @Mykle1. I see what you are getting at, and I’ve been looking at the same thing, just not sure how to call them up individually at intervals…

          "Life's Too Short To Dance With Ugly People"
          Jim Hallock - 1995

          1 Reply Last reply Reply Quote 0
          • justjim1220J Offline
            justjim1220 Module Developer @strawberry 3.141
            last edited by

            @strawberry-3-141

            Oh, and I think it needs to be placed without spaces…

            [“Cats”,“Bats”,“Bears”]

            Not sure if that makes a difference with what you are pointing out, but I think it does.

            "Life's Too Short To Dance With Ugly People"
            Jim Hallock - 1995

            1 Reply Last reply Reply Quote 0
            • justjim1220J Offline
              justjim1220 Module Developer @strawberry 3.141
              last edited by

              @strawberry-3-141

              This Is what I’ve been working on so far…

              getLiforms: function () {
              		var url = "http://eol.org/api/pages/1.0.json?batch=false&id=";
              
              		var requestUrls = [];
              		var LifeformsArray = this.config.Lifeforms.split(",");
              
              		LifeformssArray.forEach(function(lifeform) {
              			var requestUrl = url + lifeform;
              			requestUrls.push(requestUrl);
              		}
              

              Am I even close to the right track???

              "Life's Too Short To Dance With Ugly People"
              Jim Hallock - 1995

              strawberry 3.141S 1 Reply Last reply Reply Quote 0
              • justjim1220J Offline
                justjim1220 Module Developer @strawberry 3.141
                last edited by

                @strawberry-3-141

                Yours looks better!
                :smiling_face_with_sunglasses:

                "Life's Too Short To Dance With Ugly People"
                Jim Hallock - 1995

                1 Reply Last reply Reply Quote 0
                • strawberry 3.141S Offline
                  strawberry 3.141 Project Sponsor Module Developer @justjim1220
                  last edited by

                  @justjim1220 It looks like you are trying to do multiple request, as mykles description sounds like one request with a comma seperated list.

                  Please create a github issue if you need help, so I can keep track

                  justjim1220J Mykle1M 2 Replies Last reply Reply Quote 1
                  • justjim1220J Offline
                    justjim1220 Module Developer @strawberry 3.141
                    last edited by

                    @strawberry-3-141

                    Yeah, I got that while trying to write what I had going. Your example looks more correct.

                    So, now I’m looking at your example but not exactly sure where to insert it into the code to get it to work.

                    in the config.js, I am looking at the lifeforms array as such:

                    lifeForms: "Striped Bass,Praying Mantis,Tigers,Hummingbirds,Venus Flytrap,Fox,Bald Eagle",
                    

                    then I added this to the defaults in the MMM-EOL.js:

                    lifeForms: ["", "", ""],
                    

                    then I added your example to the start function:

                            var lifeFormIds = this.config.lifeForms.split(',');
                            var lifeFormIds =[];
                            for (let lifeForm of lifeForms) {
                                if (this.config.lifeFormArray[lifeForm]) {
                                    lifeFormIds.push(this.config.lifeFormArray[lifeForm]);
                                }
                            }
                    
                            //  Set locale.
                            this.url = "http://eol.org/api/pages/1.0.json?batch=false&id=" + this.config.lifeFormIds.join(',') + 
                    

                    I know this is definitely NOT right. This is where I am getting confused…
                    Having 2 separate < var lifeFormIds > declared doesn’t look right.
                    And, placing the ‘join’ part after the URL doesn’t look right either…

                    "Life's Too Short To Dance With Ugly People"
                    Jim Hallock - 1995

                    1 Reply Last reply Reply Quote 0
                    • S Offline
                      sdetweil
                      last edited by sdetweil

                      ok, your source ‘lifeFormArray’ is a hash… good for looking up randomly

                      now to get the user specified list into something usable, split is the way as already posted

                      // if user specified something
                      if(this.config.lifeForms.length >0 )
                      {
                           // split will return an array with only one entry if no comma found
                           this.config.user_keys=this.config.lifeForms.split(',');
                      }
                      else
                           // add an entry to a special lifeFormArray entry for missing config info
                           this.config.user_keys.push("none");
                      

                      now, you want random, or walk the array of user_keys?

                      function getRandomInt(max) {
                        return Math.floor(Math.random() * Math.floor(max));
                      } 
                      
                      // get one of the user specified data keys, using random
                      var key=this.config.user_keys[this.getRandomInt(this.config.user_keys.length)];
                      
                      // append that id value to the url.
                      this.url = "http://eol.org/api/pages/1.0.json?batch=false&id=" +this.lifeFormArray.key;
                      

                      its unclear if u wanted all or just one…

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      justjim1220J 1 Reply Last reply Reply Quote 1
                      • justjim1220J Offline
                        justjim1220 Module Developer @sdetweil
                        last edited by

                        @sdetweil

                        Random would be great!

                        I’ll give it a try, Thanks!

                        "Life's Too Short To Dance With Ugly People"
                        Jim Hallock - 1995

                        S 1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 1 / 2
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Sam, technical setup by Karsten.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy