Is it possible to get the index that you are sorting in Underscore.js?

I use the JS Underscore library and in particular using _.each and _.sortby . I am wondering if there is any possible way to get the index of a value inside an iterator delegate

 _.sortBy([1, 4, 2, 66, 444, 9], function(num){ /*It'd be great to have access to the index in here */ return Math.sin(num); }); 
+79
javascript
Aug 29 '12 at 14:08
source share
6 answers

The index is indeed available, for example:

 _.sortBy([1, 4, 2, 66, 444, 9], function(num, index){ }); 
+130
Aug 29 2018-12-12T00:
source share

You can get the index of the current iteration by adding another parameter to your function iterator, for example.

 _.each(['foo', 'bar', 'baz'], function (val, i) { console.log(i + ": " + val); // 0: foo, 1: bar, 2: baz }); 
+76
Aug 29 2018-12-12T00:
source share

If you prefer to convert your array, then the iterator parameter of the map underscore function is also passed by index as the second argument. So:

 _.map([1, 4, 2, 66, 444, 9], function(value, index){ return index + ':' + value; }); 

... returns:

 ["0:1", "1:4", "2:2", "3:66", "4:444", "5:9"] 
+18
Mar 13 '14 at 7:32
source share

The _.each iterator _.each called with three parameters (element, index, list) . So yes, for _.each you get a calculator.

You can do the same in sortBy

+8
Aug 29 2018-12-12T00:
source share

I think it's worth mentioning how Underscore _.each () works internally. _.each (list, iteratee) checks if the passed list is an array object or an object.

In the case where the list is an array, iteratee arguments will be a list item and an index, as in the following example:

 var a = ['I', 'like', 'pancakes', 'a', 'lot', '.']; _.each( a, function(v, k) { console.log( k + " " + v); }); 0 I 1 like 2 pancakes 3 a 4 lot 5 . 

On the other hand, if list is an object, iteratee will accept a list item and a key:

 var o = {name: 'mike', lastname: 'doe', age: 21}; _.each( o, function(v, k) { console.log( k + " " + v); }); name mike lastname doe age 21 

For reference, this is the code _.each () from Underscore.js 1.8.3

 _.each = _.forEach = function(obj, iteratee, context) { iteratee = optimizeCb(iteratee, context); var i, length; if (isArrayLike(obj)) { for (i = 0, length = obj.length; i < length; i++) { iteratee(obj[i], i, obj); } } else { var keys = _.keys(obj); for (i = 0, length = keys.length; i < length; i++) { iteratee(obj[keys[i]], keys[i], obj); } } return obj; }; 
+3
Dec 09 '15 at 16:44
source share

In the general case, in most cases, underlining functions that take a list and an argument as the first two arguments provide access to the list index as the last argument to the iterator. This is an important distinction when it comes to two underscore elements: _.reduce and _.reduceRight, which take the β€œmemo” as the third argument β€” in the case of the two, the index will not be the second argument, but the third:

 var destination = (function() { var fields = ['_333st', 'offroad', 'fbi']; return _.reduce(waybillInfo.destination.split(','), function(destination, segment, index) { destination[fields[index]] = segment; return destination; }, {}); })(); console.log(destination); /* _333st: "NYARFTW TX" fbi: "FTWUP" offroad: "UP" The following is better of course but not demonstrate my point: var destination = _.object(['_333st', 'offroad', 'fbi'], waybillInfo.destination.split(',')); */ 

So, if you wanted, you could get the index using the underscore: _.last(_.initial(arguments)) . A possible exception (I have not tried) is _.map, since it can take an object instead of a list: "If the list is a JavaScript object, the arguments of the iterator will be (value, key, list)". - see: http://underscorejs.org/#map

+1
Sep 11 '13 at 16:41
source share



All Articles