반응형
설명
jQuery.each(function)
배열을 순환하며 원소 값을 출력
<script>
var colors = ['red', 'black', 'yellow', 'green'];
$(colors).each(function(index, value){
console.log("index: ${index} / value: {$value}");
});
/*
0 / red
1 / black
2 / yellow
3 / green
*/
</script>
배열을 순환하며 원소 값이 9가 나오기 전까지 출력
<script>
var numbers = [5, 33, 21, 9, 61, 224];
$(numbers).each(function(index, value){
if(value==9) return false; // break
console.log("index: ${index} / value: {$value}");
});
/*
0 / 5
1 / 33
2 / 21
*/
</script>
배열을 순환하며 원소 값 3을 제외하고 출력
<script>
var nums = [5, 3, 48, 2];
$(nums).each(function(index, value){
if(value==3) return; // continue
console.log("index: ${index} / value: {$value}");
});
/*
0 / 5
2 / 48
3 / 2
*/
</script>
반응형