HISE Logo Forum
    • Categories
    • Register
    • Login

    How can I efficiently remove a key from an object?

    Scheduled Pinned Locked Moved Scripting
    7 Posts 3 Posters 60 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.
    • VirtualVirginV
      VirtualVirgin
      last edited by VirtualVirgin

      "delete" does not work as expected:

      Screenshot 2025-06-23 at 6.34.14 PM.png

      There are some methods to iterate over the keys in the object and remove one and return a copy of the original, but that seems a little excessive and painful for that task at hand. Is there something simple like "delete"?

      You can listen to my orchestral mockups here:
      https://www.virtualvirgin.net/

      1 Reply Last reply Reply Quote 0
      • VirtualVirginV
        VirtualVirgin
        last edited by VirtualVirgin

        So this works but it's just a little clunky:

        var testObject = {"testKey1": "testValue1", "testKey2": "testValue2"};
        
        // removes a key from an object, but just be aware that it does not edit the object in place
        // you must use the new object made here to replace the original
        inline function objectRemoveKey(obj, keyToRemove)
        {
            local newObj = {};
        
            for (key in obj)
            {
                if (key != keyToRemove)
                {
                    newObj[key] = obj[key];
                }
            }
            
            return newObj;
        }
        
        var testRemove = objectRemoveKey(testObject, "testKey1");
        
        Console.print(trace(testRemove));
        

        You can listen to my orchestral mockups here:
        https://www.virtualvirgin.net/

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

          @VirtualVirgin You can't, as you've discovered you have to recreate the object. I usually just set the value to undefined instead.

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

          Christoph HartC 1 Reply Last reply Reply Quote 1
          • Christoph HartC
            Christoph Hart @d.healey
            last edited by

            @d-healey can it be done in plain JS? I can clone that syntax and add it in HiseScript, as this is actually a valid request.

            Christoph HartC 1 Reply Last reply Reply Quote 0
            • Christoph HartC
              Christoph Hart @Christoph Hart
              last edited by

              Haha so yeah delete key is the syntax, but that feels somewhat weird…

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

                @Christoph-Hart Maybe make it match the array remove method.

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

                Christoph HartC 1 Reply Last reply Reply Quote 1
                • Christoph HartC
                  Christoph Hart @d.healey
                  last edited by

                  @d-healey yeah something like JSON.delete(obj, key) is the easiest way, the delete syntax would require a real brain-twister with the current parser.

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

                  37

                  Online

                  1.8k

                  Users

                  12.0k

                  Topics

                  104.4k

                  Posts