Javascript 是否可以在 Underscore.js 中获取您正在排序的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12179855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to get the index you're sorting over in Underscore.js?
提问by contactmatt
I'm using the JS library Underscoreand in particular using the _.each
and _.sortby
library calls. I'm wondering if there's any possible way to get the index of the value within the iterator delegate
我正在使用 JS 库Underscore,特别是使用_.each
和_.sortby
库调用。我想知道是否有任何可能的方法来获取迭代器委托中的值的索引
_.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);
});
回答by osoner
Index is actually available like;
索引实际上是可用的;
_.sortBy([1, 4, 2, 66, 444, 9], function(num, index){ });
回答by jabclab
You can get the index of the current iteration by adding another parameter to your iterator function
, e.g.
您可以通过向迭代器添加另一个参数来获取当前迭代的索引function
,例如
_.each(['foo', 'bar', 'baz'], function (val, i) {
console.log(i + ": " + val); // 0: foo, 1: bar, 2: baz
});
回答by Martin Dow
If you'd rather transform your array, then the iterator
parameter of underscore's map
function is also passed the index as a second argument. So:
如果您更愿意转换数组,那么iterator
下划线map
函数的参数也将索引作为第二个参数传递。所以:
_.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"]
回答by janith
The iterator of _.each
is called with 3 parameters (element, index, list)
. So yes, for _.each
you cab get the index.
的迭代器_.each
使用 3 个参数调用(element, index, list)
。所以是的,为_.each
您驾驶室获取索引。
You can do the same in sortBy
你可以在 sortBy 中做同样的事情
回答by mszymulanski
I think it's worth mentioning how the Underscore's _.each() works internally. The _.each(list, iteratee) checks if the passed list is an array object, or an object.
我认为值得一提的是 Underscore 的 _.each() 在内部是如何工作的。_.each(list, iteratee) 检查传递的列表是数组对象还是对象。
In the case that the list is an array, iteratee arguments will be a list element and index as in the following example:
如果列表是数组,则 iteratee 参数将是列表元素和索引,如下例所示:
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 the list argument is an object the iteratee will take a list element and a key:
另一方面,如果 list 参数是一个对象,则迭代器将采用一个列表元素和一个键:
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 _.each() code from Underscore.js 1.8.3
作为参考,这是来自 Underscore.js 1.8.3 的 _.each() 代码
_.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;
};
回答by Dexygen
More generally, under most circumstances, underscore functions that take a list and argument as the first two arguments, provide access to the list index as the next to last argument to the iterator. This is an important distinction when it comes to the two underscore functions, _.reduce and _.reduceRight, that take 'memo' as their third argument -- in the case of these two the index will not be the second argument, but the third:
更一般地,在大多数情况下,将列表和参数作为前两个参数的下划线函数提供对列表索引的访问,作为迭代器的倒数第二个参数。当涉及到两个下划线函数 _.reduce 和 _.reduceRight 时,这是一个重要的区别,它们将 'memo' 作为第三个参数——在这两个函数中,索引不是第二个参数,而是第三:
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 underscore itself: _.last(_.initial(arguments))
. A possible exception (I haven't tried) is _.map, as it can take an object instead of a list: "If list is a JavaScript object, iterator's arguments will be (value, key, list)." -- see: http://underscorejs.org/#map
所以,如果你想,你可以使用下划线本身获得的指标:_.last(_.initial(arguments))
。一个可能的例外(我没试过)是 _.map,因为它可以接受一个对象而不是一个列表:“如果列表是一个 JavaScript 对象,迭代器的参数将是(值、键、列表)。” -- 见:http: //underscorejs.org/#map
回答by Sir_Mapsalot
When available, I believe that most lodash array functions will show the iteration. But sorting isn't really an iteration in the same way: when you're on the number 66, you aren't processing the fourth item in the array until it's finished. A custom sort function will loop through an array a number of times, nudging adjacent numbers forward or backward, until the everything is in its proper place.
如果可用,我相信大多数 lodash 数组函数都会显示迭代。但是排序并不是真正的迭代:当你在数字 66 上时,你不会处理数组中的第四项,直到它完成。自定义排序函数将循环遍历数组多次,向前或向后轻推相邻数字,直到所有内容都位于正确的位置。