
<화살표 함수 방식>
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
결과
"a"
"b"
"c"
<함수선언방식>
const arr = [1, 2, 3, 4];
arr.forEach(function (x) {
console.log(x);
});
결과
1
2
3
4
const arr = [1, 2, 3, 4];
const newArr = arr.map((x) => {
return x * 2;
});
console.log(newArr);
결과
[2, 4, 6, 8]
const arr = [1, 2, 3, 4];
let number = 3;
console.log(arr.includes(number));
결과
true
indexOf()
-> 배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고 존재하지 않으면 -1을 반환합니다.
findIndex()
-> 주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환합니다. 만족하는 요소가 없으면 -1을 반환합니다.
const arr = {
{ color: "red" },
{ color: "black" },
{ color: "blue" },
{ color: "green" }
};
let number = 3;
console.log(arr.findIndex((x) => x.color === "red"));
결과
0
일치하는 요소가 여러 개 인 경우에는 제일 첫번째 것을 출력