Iterate over a collection of objects in javascript and return true from that function
Your collection is an object and not an array, so this would be a way to do it:
var present_user = function(user){
for (var k in collection) {
if (collection[k]['screen_name'] == user) return true;
}
return false;
};
If your outer object keys are all numbers, you should be using an array instead:
var collection = [{screen_name:"justin"}, {screen_name:"barry"}];
Then iterate with:
function present_user(user) {
for(var i=0; i < collection.length; i++) {
if(collection[i].screen_name === user) return true;
}
}
You could loop the object collection too (with for..in
, see mVChr's answer), but in this case it looks like you really should be using an array.