반응형
array.find(); 는 찾는 값 단 하나를 numbers 로 걍 출력
array.filter(); 는 find 처럼 v==35; 이렇게 특정 하나의 요소를 출력 가능but 배열로 출력됨
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
let arr=[25,26,27,28,29,30,31,32,33,34,35,36];
let arr2 = [];
let arr3 = [];
let arr4 = [];
arr2 = arr.filter((v)=>{
return v>30;
});
console.log(arr2); //(6) [31, 32, 33, 34, 35, 36]
arr3 = arr.filter((v)=>{
return v<30;
});
console.log(arr3); //(5) [25, 26, 27, 28, 29]
//forEach 문으로 돌림.
arr.forEach((v)=>{
if(v>30){
arr4.push(v);
}
});
console.log(arr4); //(6) [31, 32, 33, 34, 35, 36]
let array = [1,2,3,4,5,6,7,8,9,10];
let array2 = [];
let array3 = [];
array.forEach((v)=>{
if(v>5){
array2.push(v);
}
});
console.log(array2); //(5) [6, 7, 8, 9, 10]
array3 = array.filter((v)=>{
return v<5;
});
console.log(array3); //(4) [1, 2, 3, 4]
let arrayy = [3,4,6,8,6,4,223,35,73];
let arrayy2 = [];
let arrayy3 = [];
let arrayy4 = [];
let arrayy5 = [];
arrayy.forEach((v)=>{
if(v>30){
arrayy2.push(v);
}
});
console.log(arrayy2); //(3) [223, 35, 73]
arrayy2.sort(function(a,b){
return a-b;
});
console.log(arrayy2); //(3) [35, 73, 223]
arrayy3 = arrayy.filter((v)=>{
return v >30;
});
console.log(arrayy3); //(3) [223, 35, 73]
arrayy3.sort(function(a,b){
return b-a;
});
console.log(arrayy3); //(3) [223, 73, 35]
arrayy4 = arrayy.find((v)=>{
return v==3;
});
console.log(arrayy4); //3 그 값만 반환함. 근데 그냥 number 로 출력
//없는 값을 찾으면 undefined
arrayy5 = arrayy.filter((v)=>{
return v==35;
});
console.log(arrayy5); // [35] filter 는 배열로 출력
// 없는 값을 찾으면 [] 빈 배열 출력
let arr10 = [4,6,7,3,3,6,7,88,4,10];
let arr11 = [];
let count = 0;
arr11 = arr10.find((v)=>{
count++;
return v==88;
});
console.log(count,arr11); //8 88
</script>
</body>
</html>
Array.map();
반응형