/* 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">"' + s + '"') + '</span><span class="fn">)</span>; <span class="cmt">// result [ ' + i.toString() + ' ]' + (n || '') + '</span><br>'; r[p ? 'prepend' : 'append'](st); }, s = ["red","green","blue","red","yellow","blue","green","purple","red"], t = [0,1,2,3,4,5,6,7,4,2,6,7,11,12,4,7]; i = [].allIndexOf(""); // result [ -1 ] show('[]', '', i, false, ' - expected; "".indexOf("") is 0'); i = [].allIndexOf("red"); // result [ -1 ] show('[]', 'red', i); i = ["a","b","c","d"].allIndexOf(""); // result [ -1 ] show('["a","b","c","d"]', '', i); // add variable to code block r.append('<br>var s = [<span class="str"> "' + s.join('"</span>,<span class="str">"') + '"</span> ];<br>'); i = s.allIndexOf('r'); // result [ -1 ] show('s', 'r', i); i = s.allIndexOf('red'); // result [ 0,3,8 ] show('s', 'red', i); i = s.allIndexOf('blue'); // result [ 2,5 ] show('s', 'blue', i); // add variable to code block r.append('<br>var t = [<span class="num"> ' + t.join('</span>,<span class="num">') + '</span> ];<br>'); i = t.allIndexOf("4"); // result [ -1 ] show('t', "4", i); i = t.allIndexOf(4); // result [ 4,8,14 ] show('t', 4, i); r.prepend('<br>'); p = true; // prepend user added examples $('button').click(function(){ var st = $('.array').val().replace(/(\s+)?,(\s+)?/g,',').split(','), ss = $('.search').val(); show( '[<span class="str"> "' + st.join('"</span>,<span class="str">"') + '"</span> ]', ss, st.allIndexOf(ss) ); }); });
<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>
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; }