Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- level1
- dfs
- 백준
- BFS
- 그리디
- level0
- python
- 가상메모리
- 구현
- N과M
- 재귀
- 스택
- 딕셔너리
- level3
- 가상메모리 관리
- 힙
- 운영체제
- MYSQL
- 다이나믹 프로그래밍
- programmers
- 코딩테스트
- BOJ
- 브루트포스
- 수학
- level2
- 파이썬
- DP
- 프로그래머스
- dict
- 다익스트라
Archives
- Today
- Total
동캄의 코딩도장
JS [array] 본문
'use strict'
// Array
// 1. Declaration
const arr1 = new Array();
const arr2 =[1,2];
// 2. Index position
const fruits =['apple','banana'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
// 3. Looping over an array
// a. for
for (let i=0;i<fruits.length;i++){
console.log(fruits[i]);
}
// b. for of
for (let fruit of fruits){
console.log(fruit)
}
// c. foreach
fruits.forEach((fruit)=>console.log(fruit));
// 4. Addition, deletion, copy
// push: add an item to the end
fruits.push('starwberry','peach');
//pop: remove an item from the end
fruits.pop();
fruits.pop();
console.log(fruits);
//unshift: add an item to the beginning
fruits.unshift('lemon','blueberry');
console.log(fruits);
//shift: remove an item from the beginning
fruits.shift();
console.log(fruits);
// note!! shift, unshift are slower than pop, push
// splice: remove an item by index position
fruits.push('peach','lemon','blueberry');
console.log(fruits);
fruits.splice(1,1); // index 1 삭제
console.log(fruits)
fruits.splice(1,1,'melon','watermelon'); // index 1 삭제 후, melon watermelon 추가
//combine two arrays
const fruits2=['pear','coconut'];
const newFruits=fruits.concat(fruits2)
fruits.pop();
// 5. searching
console.log(fruits)
console.log(fruits.indexOf('melon'));
console.log(fruits.indexOf('potato')); //없으면 -1 리턴
//includes
console.log(fruits.includes('apple')); //있으면 true 리턴
console.log(fruits.includes('potato')); //없으면 false 리턴
// lastIndexOf
fruits.push('apple');
console.log(fruits);
console.log(fruits.lastIndexOf('apple')); // 마지막 원소의 index 리턴
console.log(fruits.toString());
// console.log(fruits.join('j'));
fruits.reverse();
console.log(fruits);
'front > HTML&CSS&JS' 카테고리의 다른 글
JS [JSON] (0) | 2022.01.25 |
---|---|
JS [array API] (0) | 2022.01.24 |
JS [Object] (0) | 2022.01.24 |
JS [Class] (0) | 2022.01.24 |
JS [function] (0) | 2022.01.23 |