使用数组内容作为函数参数

时间:2017-08-30 21:59:19

标签: javascript node.js

如何从数组中获取所有字符串,遍历它们,然后执行所有字符串的函数Ex。

var array = ["Congrats!", "Sorry you failed"]

responses.get(array).function
// I want it to basically do is  responses.get("Congrats!").function
// and responses.get("Sorry you failed").function both but simpler

1 个答案:

答案 0 :(得分:0)

您可能想要Array.prototype.map()

var array = [ 'Congrats!', 'Sorry you failed' ]

array.map( console.log )

// Output:

// Congrats! 0 ["Congrats!", "Sorry you failed"]
// Sorry you failed 1 ["Congrats!", "Sorry you failed"]

array.map( function( str ) { console.log( str ) } )

// Output:

// Congrats!
// Sorry you failed