How to copy contents of an array to a variable

Thom

Sorry for the basic question, but I'm new to coffeescript and the docs aren't making sense to me.

I am referring to the code in this documentation: http://coffeescript.org/#loops Specifically,

# Fine five course dining.
courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
menu = (i, dish) -> "Menu Item #{i}: #{dish}" 
menu i + 1, dish for dish, i in courses

I want courses instead to be pizzas:

pizzas = ["Veggie", "Cheese", "Pepperoni", "Combo"]
menu = (i, pizza) -> "#{i}) #{pizza}" 
menu i + 1, pizza for pizza, i in pizzas

And then store the results in a variable, so that the variable contains "1) Veggie 2) Cheese", etc. Do I want to do:

pizzas = ["Veggie", "Cheese", "Pepperoni", "Combo"]
menu = (i, pizza) -> "#{i}) #{pizza}" 
menuOptions = menu i + 1, pizza for pizza, i in pizzas

I tried running this in one of the script windows, but couldn't get it to work for me and I'm finding the syntax confusing.

I tried running this with console.log menuOptions and got:

4) Combo
coreyward

Your code works (no syntax errors), but because of the operator precedence, your last line executes like this:

(menuOptions = menu) i + 1, pizza for pizza, i in pizzas

What you want is to wrap your expression in parentheses so the interpreter does what you want:

menuOptions = (menu i + 1, pizza for pizza, i in pizzas)

console.log menuOptions.join(", ")
#=> 1) Veggie, 2) Cheese, 3) Pepperoni, 4) Combo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to copy contents of the const char* type variable?

From Dev

Trying to copy contents of array

From Dev

How to copy contents in an array of object into another array in C++?

From Dev

copy the contents of a variable in a text file

From Dev

How to copy the contents of one 2d array to another? (VBA)

From Dev

How to copy contents of Excel UsedRange into an array for faster reading?

From Dev

How to copy a csv file contents and rewrite with additional array in the same file?

From Dev

How to copy the contents of ByteBuffer?

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

Copy contents of a unique pointer array in the copy constructor

From Dev

how to copy the variable into one dimesional array in tcl

From Dev

How do I use the contents of an array as a variable name?

From Dev

How can add in data of "file_put_contents" an array and a variable?

From Dev

How to copy json array variable data to php variable?

From Dev

How do I copy fields names and their contents to another struct variable in matlab

From Dev

How to shift the contents of an array?

From Dev

How to shuffle the contents of an array

From Dev

How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

From Dev

How to Copy Contents in one DataGridview to another DataGridview

From Dev

How to copy NSData contents to temporary file?

From Dev

How to copy contents of one canvas to another?

From Dev

How to copy contents of a baseline in Clearcase UCM?

From Dev

How to Copy Contents in one DataGridview to another DataGridview

From Dev

How to copy the contents of a label in Base clearcase VOB?

From Dev

How to copy paste contents in the vi editor

From Dev

How to copy only folder contents with WinSCP script

From Dev

Changing contents of array using outside variable

From Dev

Changing contents of array using outside variable

From Dev

How to set a variable identifier as the contents of another variable?

Related Related

  1. 1

    How to copy contents of the const char* type variable?

  2. 2

    Trying to copy contents of array

  3. 3

    How to copy contents in an array of object into another array in C++?

  4. 4

    copy the contents of a variable in a text file

  5. 5

    How to copy the contents of one 2d array to another? (VBA)

  6. 6

    How to copy contents of Excel UsedRange into an array for faster reading?

  7. 7

    How to copy a csv file contents and rewrite with additional array in the same file?

  8. 8

    How to copy the contents of ByteBuffer?

  9. 9

    Copy contents of a unique pointer array in the copy constructor

  10. 10

    Copy contents of a unique pointer array in the copy constructor

  11. 11

    how to copy the variable into one dimesional array in tcl

  12. 12

    How do I use the contents of an array as a variable name?

  13. 13

    How can add in data of "file_put_contents" an array and a variable?

  14. 14

    How to copy json array variable data to php variable?

  15. 15

    How do I copy fields names and their contents to another struct variable in matlab

  16. 16

    How to shift the contents of an array?

  17. 17

    How to shuffle the contents of an array

  18. 18

    How to properly copy contents of one char array into another when passed to function, WITHOUT using STL?

  19. 19

    How to Copy Contents in one DataGridview to another DataGridview

  20. 20

    How to copy NSData contents to temporary file?

  21. 21

    How to copy contents of one canvas to another?

  22. 22

    How to copy contents of a baseline in Clearcase UCM?

  23. 23

    How to Copy Contents in one DataGridview to another DataGridview

  24. 24

    How to copy the contents of a label in Base clearcase VOB?

  25. 25

    How to copy paste contents in the vi editor

  26. 26

    How to copy only folder contents with WinSCP script

  27. 27

    Changing contents of array using outside variable

  28. 28

    Changing contents of array using outside variable

  29. 29

    How to set a variable identifier as the contents of another variable?

HotTag

Archive