250x250
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 |
Tags
- lay 과거분사
- python virtual environment create window
- VScode에서 들여쓰기 해제 하는 방법
- 왕초보 자바스크립트
- lay 과거형
- lie 과거형
- 변수
- 자바스크립트 배열
- python virtual environment create mac
- Math.random
- javascript for
- JavaScript
- lie와 lay의 차이점
- lie 과거분사
- 자바스크립트 객체
- 대입연산자
- boolean
- VScode 주석 단축기 ctrl + / 안될때
- abc뉴스 영어공부
- 영어뉴스 영어공부
- fizzbuzz
- If
- DOM
- lie와 lay 비교
- Hot springs tourism
- 지하온천에 대한 뉴스로하는 영어 공부
- shift + tab
- 자바스크립트
- 자바스크립트 for
- 자바스크립트 데이터타입
Archives
- Today
- Total
김숭늉 마음대로
자바스크립트 배열 (Array), 배열의 길이(Array length), 배열에서 함수 이용 본문
728x90
자바스크립트 배열 생성하기 (javascript array)
자바스크립트에서 배열을 생성하는 방식은 두가지가 있습니다. [] 안에 값을 작성하는 법과, new array 함수를 통하여 배열을 생성하는 방식입니다.
const friends = ["Michael", "Steven", "Peter"]; // 첫번째 방식
console.log(friends);
const y = new Array(1991, 1984, 2008, 2020); // 두번째 방식
console.log(friends[0]);
자바스크립트 배열의 길이 (javascript array length)
자바스크립트에서 배열의 길이를 알아보는 법은 .length 를 통해 가능합니다.
const friends = ["Michael", "Steven", "Peter"];
console.log(friends.length); // 3
console.log(friends[friends.length - 1]); // Peter
배열에 새값을 추가하는것이 가능하며, const(변하지않는 값)일지라도 array의 경우는 예외입니다.
const friends = ["Michael", "Steven", "Peter"];
friends[2] = "jay";
console.log(friends); // array에서는 언제든 변경 됨
자바스크립트 배열에서 함수의 이용 (javascript array function usage)
함수의 파라미터에는 배열을 넣을수가 없습니다.
따라서 아래 ages의 변수처럶, 배열의 순서를 지정하여 고유 값을 지정하는 방식으로 사용할수 있습니다.
const calAge = function (birthyear) {
return 2037 - birthyear;
};
const years = [1990, 1967, 2002, 2010, 2018];
console.log(calAge(years)); // NaN
const age1 = console.log(calAge(years[0]));
const age2 = console.log(calAge(years[1]));
const age3 = console.log(calAge(years[years.length - 1]));
const ages = [
calAge(years[0]),
calAge(years[2]),
calAge(years[years.length - 1]),
];
console.log(ages);
자바스크립트 배열의 매서드(javascript array method)
element 추가 메서드에는 push(배열뒷쪽에 데이터 삽입)와 unshift(배열앞쪽에 데이터 삽입) 메서드가 있고,
element 삭제 메서드에는 pop(배열 뒷쪽에 데이터 삭제),shift(배열 앞쪽에 데이터 삭제) 메서드가 있다.
const friends = ["Michael", "Steven", "Peter"];
//element 추가
const newLength = friends.push("jay"); // 새변수의 길이가 필요한경우
friends.push("seungyeon");
console.log(friends); // ['Michael', 'Steven', 'Peter', 'jay', 'seungyeon']
console.log(newLength); // 4
friends.unshift("John");
console.log(friends); // ['John', 'Michael', 'Steven', 'Peter', 'jay', 'seungyeon']
//remove elements
friends.pop(); // last
const popped = friends.pop();
console.log(popped); // jay
console.log(friends); // ['John', 'Michael', 'Steven', 'Peter']
friends.shift(); // first
console.log(friends); // ['Michael', 'Steven', 'Peter']
console.log(friends.indexOf("Steven")); // 1
console.log(friends.indexOf("bob")); // -1
friends.push(23);
console.log(friends.includes("Steven")); // true (대소문자 잘 확인하기 !)
console.log(friends.includes("bob")); // false
console.log(friends.includes("23")); // false
console.log(friends.includes(23)); // true
console.log(friends);
if (friends.includes("Peter")) {
console.log("You have a frined called Peter"); // You have a frined called Peter
}
간단한 예제 문제 !
50~300불 사이의 금액은 팁금액을 15퍼센트만 적용하고 이외는 20프로의 팁금액을 적용함
bill이 125불, 555불, 44불일때의 팁금액을 배열로 작성하고, bill 금액와 팁금액을 합하여 각 total 금액의 배열을 작성하기!
const calcTips = function (bill) {
return bill >= 50 && bill >= 300 ? bill * 0.15 : bill * 0.2;
};
const bills = [125, 555, 44];
const tips = [calcTips(bills[0]), calcTips(bills[1]), calcTips(bills[2])];
console.log(bills, tips);
const totals = [bills[0] + tips[0], bills[1] + tips[1], bills[2] + tips[2]];
console.log(bills, tips, totals);
728x90
반응형
'IT > JavaScript' 카테고리의 다른 글
자바스크립트 for, while 루프 (0) | 2023.09.20 |
---|---|
자바스크립트의 객체 (javascript object), 객체 메서드(object method) (0) | 2023.09.14 |
자바스크립트 함수 표현식과 선언식 화살표함수, 함수안에서 함수 사용하기 (0) | 2023.09.13 |
자바스크립트 스트릭트 모드 (Javascript strict mode) (0) | 2023.09.12 |
스위치 조건문(switch statement), 삼항 연산자 The Conditional (Ternary) Operator (0) | 2023.09.11 |