/    Sign up×
Community /Pin to ProfileBookmark

Array of ID values

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

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdOct 03.2007 — <i>
</i>function in_array(stack, value){

var found=false;

for(k=0;k&lt;stack.length;k++){

<i> </i> if(stack[k]==value) found=true;

}

return found;

}

var arr=['test','this','out'];

alert(in_array(arr,'test'));
Copy linkTweet thisAlerts:
@WebnerdOct 03.2007 — or this way

<i>
</i>Array.prototype.find = function(searchStr) {
var returnArray = false;
for (i=0; i&lt;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);



Which returns an array of matches. Check .length property to see if a match was found
×

Success!

Help @benrob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...