【发布时间】:2015-06-30 00:49:17
【问题描述】:
什么是 Python 的等价物(Javascript):
function wordParts (currentPart, lastPart) {
return currentPart+lastPart;
}
word = ['Che', 'mis', 'try'];
console.log(word.reduce(wordParts))
还有这个:
var places = [
{name: 'New York City', state: 'New York'},
{name: 'Oklahoma City', state: 'Oklahoma'},
{name: 'Albany', state: 'New York'},
{name: 'Long Island', state: 'New York'},
]
var newYork = places.filter(function(x) { return x.state === 'New York'})
console.log(newYork)
最后,这个:
function greeting(name) {
console.log('Hello ' + name + '. How are you today?');
}
names = ['Abby', 'Cabby', 'Babby', 'Mabby'];
var greet = names.map(greeting)
谢谢大家!
【问题讨论】:
-
reduce,map, 和filter:P 除非你在 python3 中,在这种情况下它是functools.reduce见这里:docs.python.org/2/library/functions.html -
我认为是内置函数的相同命名。
-
你的最后一个例子不是
Array.prototype.map的惯用/正确使用;你应该改用Array.prototype.forEach或;[].forEach.call -
对答案的巨大警告,在这里:列表推导和生成器似乎比
map和filter更受青睐,这些天。所以它看起来像[mutate(x) for x in list if x > 10]
标签: javascript python