Searching arrays



  • 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++)
    


  • 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...



  • you're the man!


 

2
Online

353
Users

1.1k
Topics

7.6k
Posts