2 javascript 코드
[ CS / javascript ] 2021-01-13
python -> javascript
print(str) -> console.log(str); """ comments """ -> /* comments */ # comment -> // comment type(val) -> typeof(val); for i in list: print(i) -> for (var i of list){ console.log(i); } for key in dict: print(dict[key]) -> for (var key in dict) { console.log(dict[key]); }
set -> set
mySet = set() -> var mySet = new Set(); set.add(ele) -> set.add(ele) ; set in ele -> set.has(ele) ; set.remove(ele) -> set.delete(ele); len(set) -> set.size ; list(set) -> Array.from(set)
list -> array
myList = [] -> var myArray = []; list.push(ele) -> array.push(ele); list.pop() -> array.pop(); ele in list -> list.includes(ele); #sorting list #sort with string list.sort() -> array.sort() #sort with number list.sort() -> array.sort((a,b) => a - b) -> array.sort( function(a,b) { return a - b ; } );
dict -> object
myDict = {} -> myObject = {}; dict[key] = val -> object[key] = val; dict[key] -> object[key]; -> object.key; dict.items() -> Object.entries(object) dict.keys() -> Object.keys(object); dict.values() -> Object.values(object); key in dict -> key in dict;