Data Analytics

Python List comprehensions in JavaScript (파이썬의 list comprehensions를 자바스크립트로 구현하기)

peter was here 2021. 12. 26. 08:33
728x90

아래처럼 파이썬의 list comprehensions를 이용해서 쉽게 배열(리스트)을 만들 수 있습니다.

[i for i in range(1,11)]
>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

자바스크립트로 언어를 번역하면 이렇게 됩니다.

const testList = Array.from({length:11}, (_,i) => i + 1);
>> testList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

 

728x90