Reputation: 1235
The counts I get are fetched from an api, for simplicity i've just hard coded integers to represent my problem.
I need to iterate through vars fbCount, tweetCount, and pinCount, and do something when they're over 1k.
I'm a ruby guy, pretty sure i'm making a silly move. The error I get is: uncaught type error object 18000 has no method 'each'
var fbCount = 18000,
tweetCount = 0,
pinCount = 0;
function somethingByCount(count) {
count.each(function() {
alert("firing each count")
});
}
somethingByCount(fbCount,tweetCount,pinCOunt);
Upvotes: 0
Views: 74
Reputation: 23811
well you can use an array
var fbCount = 18000,
tweetCount = 0,
pinCount = 0;
var data=new Array(fbCount,tweetCount,pinCount);
function somethingByCount(count) {
$(count).each(function() {
alert("firing each count")
});
}
somethingByCount(data);
Upvotes: 0
Reputation: 388416
use arguments to get the list of arguments
Using $.each() to iterate - cross browser
function somethingByCount() {
$.each(arguments, function(idx, val) {
alert("firing each count:" + val)
});
}
Demo: Fiddle
Using native Array.forEach() to iterate - only on modern browsers
function somethingByCount() {
Array.prototype.forEach.call(arguments, function(val, idx){
console.log("firing each count:", val)
})
}
Demo: Fiddle
Upvotes: 2
Reputation: 782105
The function expects the argument to be an array, so you have to wrap them in brackets:
somethingByCount([fbCount, tweetCount, pingCount]);
Or you could change the function to use the arguments
variable, which contains an array of all the parameters:
function somethingByCount() {
arguments.each(function() {
alert("firing each count");
});
}
Upvotes: 1