티스토리 뷰

반응형

1. 배열 초기화

var arr = Array(arraySize).fill(value);

 

ex)

var arr = Array(3).fill(false);

 

출처: https://stackoverflow.com/questions/4049847/initializing-an-array-with-a-single-value

 

2. 배열 숫자 확인

Array.length

 

ex)

console.log(clothing.length);

출처 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/length

 

3. 특정 값이 있는지 확인

Array.includes(value)

 

ex)

const array1 = [1, 2, 3];

console.log(array1.includes(2);

// expected output : true

 

출처 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

 

4. 특정 값 지우기

별도의 remove 메소드가 존재하지 않으므로 해당 인덱스를 먼저 찾은 후에 splice 를 이용해 제거한 배열을 만들어준다.

 

ex)

let a = [1, 2, 3, 4]

const idx = a.indexOf(3)

if (idx > -1) a.splice(idx, 1)

 

출처 : https://dgkim5360.tistory.com/entry/deleting-an-item-in-array-javascript

 

5. 배열의 마지막에 요소 추가

Array.push(value)

 

ex)

array.push(3)

 

6. 배열의 마지막 요소 제거

Array.pop()

 

ex)

array.pop()

 

출처 : https://webinformation.tistory.com/38

반응형

'IT > Javascript' 카테고리의 다른 글

‘uncaught referenceerror $ is not defined’ 에러 해결 방법  (0) 2021.06.03
Truthy and Falsy  (0) 2020.05.13
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함