What is the cleanest way to make Javascript do something like Python's list comprehension?
In Python if I have a list of objects whose name's I want to 'pull out' I would do this...
list_of_names = [x.name for x in list_of_objects]
In javascript I don't really see a more 'beautiful' way of doing that other than just using a for loop construct.
FYI: I'm using jQuery; maybe it has some nifty feature that makes this possible?
More specifically, say I use a jQuery selector like $('input')
to get all input
elements, how would I most cleanly create an array of all the name
attributes for each of these input
elements--i.e., all of the $('input').attr('name')
strings in an array?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…