<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Underscore.js</title> </head> <body> <script src="underscore-min.js"></script> <script> (function(){ var x; x = _.shuffle([2, 8, 10, 3]); console.log(x); })(); </script> </body> </html>
eachとmap
<script src="underscore-min.js"></script> <script> (function(){ /* _.each([2, 5, 8], function(num)){ console.log(num * 2); }); */ var x = _.map([2, 5, 8], function(num){ return num * 2; }); console.log(x); })(); </script>
find,filter,contain
<script> (function(){ var a = [2, 5, 8, 42, 12]; var x; /* x = _.find(a, function(num){ return num > 5; }); */ /* x = _.filter(a, function(num){ return num > 5; }); */ x = _.contains(a, 10); console.log(x); })(); </script>
groupBy, countBy
<script> (function(){ var a = [1, 2, 5, 8, 42, 12]; var x; /* x = _.groupBy(a, function(num){ return num % 3; }) x = _.countBy(a, function(num){ return num % 2 == 0 ? 'even' : 'odd'; }); */ x = _.sortBy(a, function(num){ return Math.sin(num); }); console.log(x); })(); </script>