Question

I was curious to see if anyone knew a of a way to reduce this javascript code:

var channels;
channels = [];

$('li.suggestions article').each(function() {
  return channels.push($(this).data('channel-id'));
});

It's really simple -- the snippet just initializes an array called "channels", iterates over some DOM elements and collects their "data-channel-id" attribute, adding it to that array.

It is something I do a lot and it would be great to have this snippet simplified further -- I'd accept a CoffeeScript answer too if there is a nice solution.

Was it helpful?

Solution

var channels = $('li.suggestions article').map(function() {
    return $(this).data('channel-id');
}).get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top