문제

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.

도움이 되었습니까?

해결책

var channels = $('li.suggestions article').map(function() {
    return $(this).data('channel-id');
}).get();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top