Array APIs - forEach()

2022. 10. 2. 19:11Javascript

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

반응형

forEach

forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
/**
 * Performs the specified action for each element in an array.
 * @param callbackfn  A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
 * @param thisArg  An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
 */
const animals = ['🐻‍', '🦁'];

animals.forEach((value, index, array) => { //세 가지 매개변수를 받는다
    console.log(value, index, array);
    //🐻‍, 0, ['🐻‍', '🦁']
    //🦁, 1, ['🐻‍', '🦁']
});

 

for

for([initialization]; [condition]; [final-expression]){}
const animals = ['🐻', '🦁'];

for(let i = 0; i < animals.length; i++){
    console.log(animals[i]);
    //🐻
    //🦁
}

 

for... of

for(variable of iterable){}
const animals = ['🐻', '🦁'];

for(let animal of animals){
    console.log(animal);
    //🐻
    //🦁
}
반응형