var channels = $('li.suggestions article').map(function() {
return $(this).data('channel-id');
}).get();
Can this javascript be reduced?
-
12-07-2023 - |
题
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.
解决方案
不隶属于 StackOverflow