• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Array] JS - fill() 메서드 - 배열값변경 (= 배열값교체 = 배열값바꾸기 = fill메서드 = 필메서드) ※지정 위치들의 배열값을 지정값으로 대체

목차
  1. array.fill() 예제 - 지정 위치의 배열값들을 변경
  2. array.fill() 정의
  3. array.fill() 구문

 

array.fill() 예제 - 지정 위치의 배열값들을 변경 

[예제1] - 모든 배열요소를 변경

 

<p id="demo"></p>


<script>

const langs = ["HTML", "CSS", "JavaScript", "jQuery"];

document.getElementById("demo").innerHTML = langs.fill("JS");

</script>

 

결과값: JS,JS,JS,JS


[예제2] - 색인번호 2부터 3 바로 앞까지만 변경 (= 색인번호 2만 변경)


<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

var codes = ["HTML", "CSS", "JavaScript", "jQuery"];

document.getElementById("demo").innerHTML = codes;


function homzzang() {  

  document.getElementById("demo").innerHTML = codes.fill("JS",2,3);

}

</script>

 

결과값: HTML,CSS,JS,jQuery


array.fill() 정의

 

지정 위치의 배열값들을 지정값으로 변경.

 


 

1.

  • fill() 메서드는 원본 배열을 덮어씀.
  • 시작과 끝 색인번호 사용해 특정 위치의 요소들을 변경 가능.
  • 시작과 끝 색인번호 없으면 모든 배열값들을 변경함.

 

2.

  • ECMAScript6 (ES6: JavaScript 2015)에서 도입. 
  • 주요 최신 브라우저 모두 지원. (단, IE11 이하는 지원 X)

 

3. MDN fill() 예제보기

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill


 

array.fill() 구문

 

array.fill(value, start, end)

 


[매개변수]

 

value

필수. 대체값

 

start

선택. 시작 색인번호 (포함 O) (기본값: 0)

 

end

선택. 끝 색인번호 (포함 X) (기본값: 배열길이)

 


[반환값]

 

대체값으로 변경된 배열 반환. (※ 원본배열을 덮어씀.)

 



분류 제목
Basic JS - Let 키워드 ★★★★★ - 변수 블럭범위 설정 (렛키워드) ※ 클로저
Modernizr JS - Modernizr (모더나이저 = 마더나이저) - 크로스브라우징 구현 (= 브라우저의 HTML5 C…
React JS - React (리액트) - JS 라이브러리 일종으로 사용자 인터페이스 구축에 사용.(= 현재시간 = …
DOM_Attribute JS - Attribute 객체 - 속성객체
Window_Console JS - window.console 객체 - 콘솔객체
DOM_Document JS - Document 객체 - 웹문서 객체의 속성/메서드 종류 (= document객체 = 다큐먼트객체)
DOM_Element JS - Element 객체 - 요소객체 (속성 + 메서드) 종류
DOM_Event JS - Event 종류 ★ - 이벤트부착 / 이벤트종류 / 이벤트속성 / 이벤트메서드
API_Geolocation JS - Geolocation - 지리위치객체종류
Window_History JS - window.history 객체 정의 + 히스토리 객체의 (속성/메서드) 종류
DOM_HTMLCollection JS - HTMLCollection 객체 (속성 + 메서드) 종류
Window_Location JS - window.location 객체 - 위치 객체 (= window.location객체 = 윈도우로케…
Window_Navigator JS - window.navigator 객체 - 네비게이터 객체 (= window.navigator객체 = …
Window_Screen JS - window.screen 객체 - 스크린 객체 (= window.screen객체 = 윈도우스크린객체…
DOM_Style JS - CSS 스타일 속성값 반환 ★★ + style 객체의 속성 종류
Window_Object JS - window 객체 - 윈도우객체 (속성/메서드) 종류
API_Storage JS - Storage Object - 스토리지 객체 (※ 데이터 저장소)
Object JS - Object Constructor - 객체생성자종류 ★
Object JS - Object Prototypes - 객체프로토타입 (= 객체원형) ★
Object JS - object ECMAScript 5 - 객체혁명
22/67
목록
찾아주셔서 감사합니다. Since 2012