Computer Programming/React-Native

앱개발 종합반_map

JYCoder 2022. 6. 27. 00:06

딸기가 총 몇 개인지 알 수 있는 방법.

다음 두 가지 방식의 결과는 같다. 첫 번째는 기존의 방식, 두 번째는 React에서 잘 쓰는 방식.

 

첫 번째,

let fruit_list = ['사과','감','감','배','포도','포도','딸기',
'포도','감','수박','딸기']

let count = 0;

for(let i=0; i<fruit_list.length; i++){
    if(fruit_list[i]=='딸기'){
        count+=1;
    }
}

console.log(count);

두 번째,

let fruit_list = ['사과','감','감','배','포도','포도','딸기',
'포도','감','수박','딸기']

let count = 0;

fruit_list.map((value, i) => {
    if(fruit_list[i]=='딸기'){
        count+=1;
    }
})

console.log(count);

 

LIST