2017-08-30 2 views
-1

Wie bekomme ich alle Zeichenfolgen aus einem Array, durchlaufen Sie sie, und es würde eine Funktion für alle von ihnen ausführen Ex.Verwenden Array-Inhalt als Funktionsparameter

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

Google 'Function.prototype.apply' – Pointy

+2

Auch die Verbreitung Syntax https://developer.mozilla.org/en/ docs/Web/JavaScript/Referenz/Operatoren/Spread_operator – locrizak

+0

foreach - https://www.w3schools.com/jsref/jsref_forEach.asp – mchan

Antwort

0

Sie könnten wollen Array.prototype.map werden()

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

Wie kann ich das Teil nach Congrats entfernen oder Sorry, dass Sie gescheitert sind –

+0

Sie können 'console.log' durch irgendeine Funktion ersetzen. Also ... 'array.map (funktion (str) {console.log (str)})' – flcoder

Verwandte Themen