Hi, I need to take an array and see if there is already an occurrence of the ID in the array. How would I go about looping though the array to see if the ID is already in there. Any help would be greatly appreciated. Cheers
<i> </i>function in_array(stack, value){ var found=false; for(k=0;k<stack.length;k++){ <i> </i> if(stack[k]==value) found=true; } return found; } var arr=['test','this','out']; alert(in_array(arr,'test'));
<i> </i>Array.prototype.find = function(searchStr) { var returnArray = false; for (i=0; i<this.length; i++) { if (typeof(searchStr) == 'function') { if (searchStr.test(this[i])) { if (!returnArray) { returnArray = [] } returnArray.push(i); } } else { if (this[i]===searchStr) { if (!returnArray) { returnArray = [] } returnArray.push(i); } } } return returnArray; } var arr=['check','this','out']; alert(arr.find('check').length);
Help @benrob spread the word by sharing this article on Twitter...