-3

I want to call each() on the resulting array of another function that I call.

Why can't I do:

callMethodThatReturnsAnArray().each(function () {
    ...
});

I get a callMethodThatReturnsAnArray(...).each is not a function error.

TylerH
  • 20,799
  • 66
  • 75
  • 101
pavlos163
  • 2,730
  • 4
  • 38
  • 82
  • Please, show the implementation of `callMethodThatReturnsAnArray()`. – Bruno Peres Aug 10 '17 at 14:27
  • Because if it is an array of js elements, the method is `forEach` – quirimmo Aug 10 '17 at 14:27
  • He may be using jQuery, which I believe does have a `.each()` method. – Matthew Brown Aug 10 '17 at 14:28
  • yes but the jquery each method is called in this way: `$.each(array, fn());` – quirimmo Aug 10 '17 at 14:29
  • that is correct, but you should use that like this : `$.each(callMethodThatReturnsAnArray(), function( ...` – Timothy Groote Aug 10 '17 at 14:29
  • @MatthewBrown: Yes, but not on arrays. – T.J. Crowder Aug 10 '17 at 14:30
  • Your syntax is fine, it's just that JavaScript arrays don't have an `each` method. You probably meant [`forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). More details in the linked question's answers. – T.J. Crowder Aug 10 '17 at 14:35
  • if you *did* want to use this syntax per se, you could just add it to the Array prototype : `Array.prototype.each = function(lambda) { for(var i =0; i < this.length; i++) { lambda(this[i]); } }` should work in your case – Timothy Groote Aug 10 '17 at 14:39
  • 1
    @TimothyGroote: It's extremely poor practice to add enumerable properties to `Array.prototype`. If you're going to suggest something like this, I strongly recommend showing how to do it without creating an enumerable property (e.g., via `Object.defineProperty` or similar). – T.J. Crowder Aug 10 '17 at 14:58
  • 1
    @T.J.Crowder guess i should have elaborated a little more, but yeah, agreed ; this is indeed *bad* practice. i was halfway through making an answer post when the question got closed, so i kinda though scr-- it. – Timothy Groote Aug 10 '17 at 15:01

2 Answers2

4

each() is jQuery for DOM elements - what are you doing exactly? For plain Javascript, you'll want to use forEach() or map()

Christopher Messer
  • 2,040
  • 9
  • 13
  • 1
    Not just for dom elements per se ;) – Timothy Groote Aug 10 '17 at 14:28
  • Indeed, jQuery objects *can*, rarely, hold other things. And jQuery has no monopoly on `each`. Still, going with the odds... – T.J. Crowder Aug 10 '17 at 14:29
  • @T.J.Crowder since jQuery arrays are little more than glorified arrays, most of the guts of jQuery counts on selector results behaving exactly like an array. vice versa, many jQuery functions can be used to work with plain arrays of just about anything – Timothy Groote Aug 10 '17 at 14:41
  • 1
    @TimothyGroote: jQuery *objects* are indeed very array-like. Re holding non-DOM elements: That's what I said. For instance, you often end up with a jQuery object (not array) as the result from [`map`](http://api.jquery.com/map/) (and then you use `get` to get the actual array -- or rather, you did a few years back, nowadays jQuery's `map` is rarely useful). – T.J. Crowder Aug 10 '17 at 14:51
0

Array has forEach method not each. Use

callMethodThatReturnsAnArray().forEach(function(entry) {
// ...do whatever you want to do

});

If you want to use each, Jquery has method

jQuery.each(array, callback )

So you can write code something like.

$.each(callMethodThatReturnsAnArray(), function(index, value){// do whatever you want to do.});
Deepak Singh
  • 411
  • 1
  • 5
  • 13