[JS] Array.from length

JeongInHuh·2023년 11월 10일
0

JS 고차함수

목록 보기
1/3

Array.from 메서드는 주어진 길이만큼의 배열을 생성하고,
두 번째 인자로 전달된 함수(fn)를 호출하여 각 요소의 값을 결정

  • 이때 fn은 중괄호를 붙이지 않아도 된다!


참조에 의해 값 변경이 일어나기 때문에, 두 출력값은 같다!
const fn = () => ({ v : 100})

// 3번 반복
const result = Array.from({ length: 3 }, fn); 

console.log(result) // [{v:300}, {v:200}, {v:100}]

result[0].v = 300
result[1].v = 200

console.log(result) // [{v:300}, {v:200}, {v:100}]

출처

profile
컴퓨터공학부 대학생. 4학년. velog 꾸미기 : https://velog.io/@ybkim3603/Velog벨로그-사용법-튜토리얼 Git컨벤션: https://velog.io/@shin6403/Git-git-커밋-컨벤션-설정하기 커리어 방향 설정 모음글:https://velog.io/@eon7500/커리어-방향성-설정에-도움되는-글

0개의 댓글