Make Javascript do List Comprehension

后端 未结 10 1216
情话喂你
情话喂你 2020-12-05 09:05

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 \'

10条回答
  •  执念已碎
    2020-12-05 09:49

    So, python's list comprehensions actually do two things at once: mapping and filtering. For example:

    list_of_names = [x.name for x in list_of_object if x.enabled]
    

    If you just want the mapping part, as your example shows, you can use jQuery's map feature. If you also need filtering you can use jQuery's "grep" feature.

提交回复
热议问题