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
。请参见此处:https://docs.python.org/2/library/functions.html - NightShadeQueenArray.prototype.map
的惯用/正确用法;你应该使用Array.prototype.forEach
或;[].forEach.call
。 - royhowiemap
和filter
。因此,以下代码似乎更受欢迎:[mutate(x) for x in list if x > 10]
。 - David Ehrmann