使用数组内容作为函数参数
问题描述:
如何从数组中获取所有字符串,遍历它们,然后执行所有这些函数。使用数组内容作为函数参数
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
答
你可能会想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
+0
如何在恭喜或遗憾失败后删除零件 –
+0
您可以用任何函数替换'console.log'。所以...'array.map(function(str){console.log(str)})' – flcoder
谷歌'Function.prototype.apply' – Pointy
还利用扩展语法https://developer.mozilla.org/en/ docs/Web/JavaScript/Reference/Operators/Spread_operator – locrizak
foreach - https://www.w3schools.com/jsref/jsref_forEach.asp – mchan