HISE Logo Forum
    • Categories
    • Register
    • Login

    search in array of objects

    Scheduled Pinned Locked Moved General Questions
    6 Posts 3 Posters 273 Views
    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.
    • ustkU
      ustk
      last edited by

      Is there a way too search an object property in an array more easily than:

      var objArray = [{"nb": 5}, {"nb": 9}];
      
      var isHere = false;
      
      for (o in objArray)
      {
          if (o.nb == 5)
          {
              isHere = true;
              break;
          }
      }
      

      thinking to something more straightforward like:

      var objArray = [{"nb": 5}, {"nb": 9}];
      
      var isHere = objArray.contains("nb" == 5);
      

      Which of course makes no sense as it is...

      I can evidently write a function but I wonder if a one-liner could exist?

      Can't help pressing F5 in the forum...

      1 Reply Last reply Reply Quote 1
      • d.healeyD
        d.healey
        last edited by

        Loop is the only way. You can reduce the lines a bit though I think. I'm off for dinner but I'll take a look when I'm back.

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        ustkU 1 Reply Last reply Reply Quote 1
        • ustkU
          ustk @d.healey
          last edited by

          @d-healey
          Something like this could do...

          var objArray = [{"nb": 5}, {"nb": 9}];
          
          var isHere = false;
          
          for (o in objArray)
              if (isHere = o.nb == 5) break;
          

          Can't help pressing F5 in the forum...

          d.healeyD 1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey @ustk
            last edited by

            @ustk Yeah I think that looks pretty good.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • N
              nani
              last edited by

              I know similar questions have been asked before, but this one is a little different. I have an array of unnamed objects, which contain an array of named objects, and I need to get the object where "name" is "string 1". Here is an example array.

              var array = [
              { name:"string 1", value:"this", other: "that" },
              { name:"string 2", value:"this", other: "that" }
              ];

              ustkU 1 Reply Last reply Reply Quote 0
              • ustkU
                ustk @nani
                last edited by ustk

                @nani

                Just to make things a bit clearer, Here you have an array with objects, but it does not contains array with named objects... It contains "objects" with "properties".
                To get only the object where the property "name" is equal to "string 1":

                var objectToCopy;
                
                for (obj in array)
                {
                    if (obj.name == "string 1")
                        objectToCopy = obj;
                }
                

                Can't help pressing F5 in the forum...

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

                23

                Online

                1.7k

                Users

                11.9k

                Topics

                103.6k

                Posts