Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js equivalent data types for python's list, tuple and dictionary

For anyone migrating from python to node.js, it will be useful to know the equivalent data types in node.js for lists, tuples and dictionaries.

Do they exist? If not, are there node.js modules to achieve equivalent data structures in node.js?

like image 543
guagay_wk Avatar asked Jan 07 '23 10:01

guagay_wk


1 Answers

Where Python has lists, JavaScript has arrays. These are declared using the exact same syntax as Python, with square brackets and commas:

var names = ["Foo", "Bar", "Baz"];
names[0]; # Foo

As an alternative to dictionaries you can use associative arrays, which are actually just JavaScript objects. Again, the syntax is similar to Python:

var dictionary = {"name": "Foo", "email": "[email protected]"};
dictionary["name"]; # Foo

JavaScript doesn't have tuples, but there is a tuple library on NPM, although I've never used it.

If you want to experiment with arrays and associative arrays, I'd recommend repl.it - the right-hand column is a REPL (read-eval-print loop), just like Python's equivalent when you open a terminal and type py.

like image 155
Aaron Christiansen Avatar answered Jan 09 '23 00:01

Aaron Christiansen