How can I put multiple elements in a JSON object using CoffeeScript?

categories = {{"code": "zzz", "title": "Electronics"}, {"code": "yyy", "title": "Cars"}}; 

What is my javascript. What is the equivalent of CoffeeScript?

+6
source share
2 answers

Brackets are optional in CoffeeScript, you can have the following:

 categories = [ code : 'zzz' title : 'Electronics' , code : 'yyy' title : 'Mechanics' ] 

(note the open comma) or more obvious:

 categories = [ { code : 'zzz' title : 'Electronics' }, // comma optional { code : 'yyy' title : 'Mechanics' } ] 
+21
source

First of all, I think your JavaScript should look like this:

 categories = [{"code": "zzz", "title": "Electronics"}, {"code": "yyy", "title": "Cars"}]; 

You need an array, right? Then CoffeeScript, well, in exactly the same way (without an end semicolon, but this is also optional in JavaScript):

 categories = [{"code": "zzz", "title": "Electronics"}, {"code": "yyy", "title": "Cars"}] 

At the top of the Github CoffeeScript page is a TRY COFFEESCRIPT button , which may be useful for such things.

+1
source

Source: https://habr.com/ru/post/897357/


All Articles