Array.allIndexOf()

Add a Array function that returns all indexes of the searchElement. It's named allIndexOf because it is an expansion of the indexOf function.

by Mottie

HTML

<script src="http://fonts.googleapis.com/css?family=Jura"></script>
<h1 class="fn">Array.allIndexOf()</h1>
<h2><em>Array.allIndexOf(searchElement)</em></h2>

<ul>
    <li>Array <span class="num">[Array]</span> - the array to search within for the searchElement</li>
    <li>searchElement <span class="str">[String]</span> - the desired element with which to find starting indexes</li>
</ul>

<h2>Test it Out</h2>
<div id="form">
    Input string becomes an array using .split(",")<br>
    <label>String:</label> <input class="array" type="text" value="red,green,blue,red,yellow,blue,green,purple,red"><br>
    <label>Search for:</label> <input class="search" type="text" value="green"><br>
    <button>Test</button>
</div>

<h2>Examples of use</h2>
<pre id="results"></pre>

CSS

body { background: #333; color: #ddd; margin: 0 8px; }
#form, pre { margin-left: 30px; font-size: 16px; }
#form { line-height: 25px; width: 300px; }
h1 { margin: 21px 0; font-size: 32px; font-weight: 700; font-family: 'Jura', sans-serif; }
h2 { margin: 19px 0; font-size: 24px; font-weight: 700; }
em { font-style: italic; }
ul { padding-left: 40px; margin: 16px 0; }
li { list-style-type: disc; }
input.string, input.search, input.array { width: 200px; float: right; }
.var { color: #ddd; }
.fn { color: #e9b965; }
.cmt { color: #999; }
.str { color: #92d050; }
.bool { color: #f00; }
.num { color: #b22; }

JavaScript

/*
Array.allIndexOf(searchElement)
  Array [Array] - the array to search within for the searchElement
  searchElement [String] - the desired element with which to find starting indexes
*/
(function(){
    Array.prototype.allIndexOf = function(searchElement) {
        if (this === null) { return [-1]; }
        var len = this.length,
        hasIndexOf = Array.prototype.indexOf, // you know, because of IE
        i = (hasIndexOf) ? this.indexOf(searchElement) : 0,
        n,
        indx = 0,
        result = [];
        if (len === 0 || i === -1) { return [-1]; }
        if (hasIndexOf) {
            // Array.indexOf does exist
            for (n = 0; n <= len; n++) {
                i = this.indexOf(searchElement, indx);
                if (i !== -1) {
                    indx = i + 1;
                    result.push(i);
                } else {
                    return result;
                }
            }
            return result;
        } else {
        // Array.indexOf doesn't exist
            for (n = 0; n <= len; n++) {
                if (this[n] === searchElement) {
                    result.push(n);
                }
            }
            return (result.length > 0) ? result : [-1];
        }
    };
})();












// *** Demo code ***
// Demo code - Array.allIndexOf()
$(function(){
    var i, r = $('#results'), p = false,
    // make it pretty, also to allow adding more examples ;)
    // w = string (no quotes means it's a variable),
    // s = search string, i = allIndexOf, c = ignore case, n = note
    show = function(w,s,i,n){
        var st = w + '.<span class="fn">allIndexOf(</span>' +
            (typeof s === 'number' ? '<span class="num">' + s : '<span class="str">&quot;' + s + '&quot') +
            '</span><span class="fn">)</span>; <span class="cmt">// result [ ' + i.toString() + ' ]' +
            (n || '') + '</span><br>';
        r[p ? 'prepend' : 'append'](st);
    },

    s =...