[JavaScript] 8. Array

2022. 2. 25. 02:03·IT/WILT

 

1.  Array Declaration

'use strict';

const arr1 = new Array();
const arr2 = [1, 2];

 

 

2. Index position

const fruits = ['apple', 'banana'];
console.log(fruits); // ['apple', 'banana']
console.log(fruits.length); // 2
console.log(fruits[0]); // apple
console.log(fruits[1]); // banana
console.log(fruits[2]); // undefined

 

 

3. Looping over an array

for (let i = 0; i < fruits.length; i++) { // for
	console.log(fruits[i]);
} // apple banana

for (let fruit of fruits) { // for of
	console.log(fruit);
} // apple banana

fruits.forEach(function (fruit, index, array) {
	console.log(fruit, index, array);
}); // 아래처럼 생략가능

fruits.forEach((fruit) => console.log(fruit));

 

 

4. Addition, Deletion and Copy

 - push: add an item to the end.

 - pop: remove an item from the end.

 - unshift: add an item to the begining.

 - shift: remove an item from the begining.

 - shift, unshift are slower than pop, push.

- splice: remove an item by index position.

fruits.push('berry');
fruits.pop();
fruits.unshift('lemon');
fruits.shift();
fruits.splice(1, 1); // .splice(startIndex, deleteCount)

 

4-1. combine two arrays

const fruits2 = ['grape', 'mog'];
const newFruits = fruits.concat(fruits2);

 

 

5. Searching

 - find the index.

console.log(fruits.indexOf('apple'); // 0
console.log(fruits.indexOf('banana'); // 1

console.log(fruits.includes('banana'); // true
console.log(fruits.includes('grape'); // false

fruits.push('apple');
console.log(fruits); // ['apple', 'banana', 'lemon', 'apple']
console.log(fruits.lastIndexOf('apple'); // 3

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

[JavaScript] 10. JSON  (0) 2022.03.02
[JavaScript] 9. Array API  (0) 2022.02.26
[JavaScript] 7. Object  (0) 2022.02.24
[JavaScript] 6. 객체지향 언어, class VS object  (0) 2022.02.22
[JavaScript] 5. 함수의 선언과 표현, Arrow Function  (0) 2022.02.22
'IT/WILT' 카테고리의 다른 글
  • [JavaScript] 10. JSON
  • [JavaScript] 9. Array API
  • [JavaScript] 7. Object
  • [JavaScript] 6. 객체지향 언어, class VS object
KimCookieYa
KimCookieYa
무엇이 나를 살아있게 만드는가
  • KimCookieYa
    쿠키의 주저리
    KimCookieYa
  • 전체
    오늘
    어제
    • 분류 전체보기 (574)
      • 혼잣말 (88)
      • TIL (2)
      • 커리어 (24)
        • Sendy (21)
        • 외부활동 기록 (2)
      • 프로젝트 (186)
        • 티스토리 API (5)
        • 코드프레소 체험단 (89)
        • Web3 (3)
        • Pint OS (16)
        • 나만무 (14)
        • 대회 (6)
        • 정글 FE 스터디 (16)
        • MailBadara (12)
        • github.io (1)
        • 인공지능 동아리, AID (5)
        • 졸업과제 (18)
        • OSSCA 2024 (1)
      • 크래프톤 정글 2기 (80)
      • IT (168)
        • 코딩 (4)
        • CS (18)
        • 에러 (5)
        • 블록체인 (23)
        • Front-End (39)
        • 알고리즘&자료구조 정리 (3)
        • 코딩테스트 (3)
        • BOJ 문제정리 (41)
        • WILT (12)
        • ML-Agents (4)
        • 강화학습 (1)
        • Android (0)
        • LLM (2)
      • 전공 (1)
        • 머신러닝 (1)
      • 자기계발 (20)
        • 빡공단X베어유 (2)
        • 독서 (15)
  • 블로그 메뉴

    • 홈
    • 방명록
    • Github
    • Velog
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Pint OS
    파이썬
    센디
    니어프로토콜
    알고리즘
    numpy
    졸업과제
    JavaScript
    리액트
    NEAR Protocol
    코드프레소
    머신러닝
    핀토스
    Flutter
    RNN
    글리치해커톤
    pintos
    딥러닝
    나만무
    블록체인
    MailBadara
    react
    해커톤
    사이드프로젝트
    부산대
    프로그래머스
    docker
    크래프톤정글
    자바스크립트
    OS
  • hELLO· Designed By정상우.v4.10.3
KimCookieYa
[JavaScript] 8. Array
상단으로

티스토리툴바