Forum

    • Register
    • Login
    • Search
    • Categories

    search in array of objects

    General Questions
    3
    6
    67
    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.
    • ustk
      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?

      I cannot help pressing F5 in the forum...
      Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

      1 Reply Last reply Reply Quote 1
      • d.healey
        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

        ustk 1 Reply Last reply Reply Quote 1
        • ustk
          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;
          

          I cannot help pressing F5 in the forum...
          Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

          d.healey 1 Reply Last reply Reply Quote 0
          • d.healey
            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" }
              ];

              ustk 1 Reply Last reply Reply Quote 0
              • ustk
                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;
                }
                

                I cannot help pressing F5 in the forum...
                Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

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

                11
                Online

                983
                Users

                6.6k
                Topics

                60.8k
                Posts