Javascript 序列的 Python 'enumerate' 的 ES6 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34336960/
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
What is the ES6 equivalent of Python 'enumerate' for a sequence?
提问by Guillaume Vincent
Python has a built-in function enumerate
, to get an iterable of (index, item)
pairs.
Python 有一个内置函数enumerate
,用于获取可迭代的(index, item)
对。
Does ES6 have an equivalent for an array? What is it?
ES6 是否有数组的等价物?它是什么?
def elements_with_index(elements):
modified_elements = []
for i, element in enumerate(elements):
modified_elements.append("%d:%s" % (i, element))
return modified_elements
print(elements_with_index(["a","b"]))
#['0:a', '1:b']
ES6 equivalent without enumerate
:
ES6 等效,没有enumerate
:
function elements_with_index(elements){
return elements.map(element => elements.indexOf(element) + ':' + element);
}
console.log(elements_with_index(['a','b']))
//[ '0:a', '1:b' ]
采纳答案by Kyle
Yes there is, check out Array.prototype.entries()
.
是的,检查一下Array.prototype.entries()
。
const foobar = ['A', 'B', 'C'];
for (const [index, element] of foobar.entries()) {
console.log(index, element);
}
回答by Thank you
Array.prototype.map
already gives you the index as the second argument to the callback procedure... And it's supportedalmost everywhere.
Array.prototype.map
已经为您提供了索引作为回调过程的第二个参数......而且几乎所有地方都支持它。
['a','b'].map(function(element, index) { return index + ':' + element; });
//=> ["0:a", "1:b"]
I like ES6 too
我也喜欢 ES6
['a','b'].map((e,i) => `${i}:${e}`)
//=> ["0:a", "1:b"]
回答by tav
let array = [1, 3, 5];
for (let [index, value] of array.entries())
console.log(index + '=' + value);
回答by James Ko
Excuse me if I'm being ignorant (bit of a newbie to JavaScript here), but can't you just use forEach
? e.g:
对不起,如果我无知(这里是 JavaScript 的新手),但你不能只使用forEach
?例如:
function withIndex(elements) {
var results = [];
elements.forEach(function(e, ind) {
results.push(`${e}:${ind}`);
});
return results;
}
alert(withIndex(['a', 'b']));
There's also naomik'sanswer which is a better fit for this particular use case, but I just wanted to point out that forEach
also fits the bill.
还有naomik 的答案更适合这个特定用例,但我只想指出这forEach
也符合要求。
ES5+ supported.
支持 ES5+。
回答by Keyvan
pythonic
offers an enumerate
function that works on all iterables, not just arrays, and returns an Iterator, like python:
pythonic
提供了一个enumerate
适用于所有可迭代对象的函数,而不仅仅是数组,并返回一个Iterator,如 python:
import {enumerate} from 'pythonic';
const arr = ['a', 'b'];
for (const [index, value] of enumerate(arr))
console.log(`index: ${index}, value: ${value}`);
// index: 0, value: a
// index: 1, value: b
DisclosureI'm author and maintainer of Pythonic
披露我是 Pythonic 的作者和维护者
回答by crifan
as Kyle
and Shanoor
say is Array.prototype.entries()
作为Kyle
和Shanoor
说的是Array.prototype.entries()
but for newbie like me, hard to fully understand its meaning.
但是对于我这样的新手来说,很难完全理解它的含义。
so here give an understandable example:
所以这里举一个可以理解的例子:
for(let curIndexValueList of someArray.entries()){
console.log("curIndexValueList=", curIndexValueList)
let curIndex = curIndexValueList[0]
let curValue = curIndexValueList[1]
console.log("curIndex=", curIndex, ", curValue=", curValue)
}
equivalent to python
code:
相当于python
代码:
for curIndex, curValue in enumerate(someArray):
print("curIndex=%s, curValue=%s" % (curIndex, curValue))
}