Python has a built-in function enumerate
, to get an iterable of (index, item)
pairs.
Python有一个内置函数枚举,以获得(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等效而不枚举:
function elements_with_index(elements){
return elements.map(element => elements.indexOf(element) + ':' + element);
}
console.log(elements_with_index(['a','b']))
//[ '0:a', '1:b' ]
6 个解决方案
#2
10
Array.prototype.map
already gives you the index as the second argument to the callback procedure... And it's supported almost 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"]
#3
3
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's answer 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.
#4
2
let array = [1, 3, 5];
for (let [index, value] of array.entries())
console.log(index + '=' + value);
#5
1
pythonic
offers an enumerate
function that works on all iterables, not just arrays, and returns a generator (yield
), like python:
pythonic提供了一个枚举函数,适用于所有迭代,而不仅仅是数组,并返回一个生成器(yield),如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
#6
0
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))
}
#1
#2
10
Array.prototype.map
already gives you the index as the second argument to the callback procedure... And it's supported almost 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"]
#3
3
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's answer 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.
#4
2
let array = [1, 3, 5];
for (let [index, value] of array.entries())
console.log(index + '=' + value);
#5
1
pythonic
offers an enumerate
function that works on all iterables, not just arrays, and returns a generator (yield
), like python:
pythonic提供了一个枚举函数,适用于所有迭代,而不仅仅是数组,并返回一个生成器(yield),如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
#6
0
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))
}