Forum

    • Register
    • Login
    • Search
    • Categories

    Searching arrays

    Scripting Forum
    2
    3
    582
    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.
    • d.healey
      d.healey last edited by

      I think it's important to have the ability to search arrays, I couldn't find a function in HISE's implementation of JS to do this so I rolled my own indexOf function and attached it to the array type but I think a native solution would be better.

      if (!Array.indexOf)
      {
      	Array.indexOf = function(obj, from, typeStrict)
      	{
      	    if (fromIndex == null)
      	    {
      	        fromIndex = 0;
      	    }
      	    else if (fromIndex < 0)
      	    {
      	        fromIndex = Math.max(0, this.length + fromIndex);
      	    }
      
      	  	for (var j = from; j < this.length; j++)
      	    {
      	    	if (typeStrict == true)
      	    	{
      				if (this[j] === obj) return j;
      	    	}
      
      	        if (this[j] == obj) return j;
      	    }
      
      	    return -1;
      	};
      }
      

      HISE didn't like it when I used something like this for my for loop, although it is in the JavaScript tutorial

      for (var j = from, len = this.length; j < len;  j++)
      

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

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

        Thanks for spotting this. The problem is that by introducing the for … in loop, I accidentally broke support for multiple initialisers and fixing this is not trivial.

        But I added a native indexOf() method. While I was at it, I also added a clean Array.isArray() method...

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

          you're the man!

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

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

          15
          Online

          1.1k
          Users

          6.8k
          Topics

          62.3k
          Posts