For문 종류
첫번째 ( For )
let html = `<ol>`;
for(let i=0; i<배열명.length; i+=1){
html += `<li>${배열명[i]}</li>`;
}
html += `</ol>`;
두번째 ( For ~ of / For ~ in )
let html = `<ol>`;
for(let item of 배열명){
html += `<li>${item}</li>`;
}
html += `</ol>`;
- For in => 객체의 속성목록을 가져올 때 사용
for(let property_name in 배열명){
console.log(배열명[property_name]);
}
let html = ``;
for(let item of 배열명){
html += `<div><ul>`;
for(let property_name in item){
html += `<li>${item[property_name]}</li>`;
}
html += `</ul></div>`;
}
세번째 ( ForEach )
let html = `<ol>`;
배열명.forEach(function(item, index, array){
html += `<li>${item}</li>`;
html += `<li>${array[index]}</li>`;
})
html += `</ol>`;
let html1 = `<ol>`;
배열명.forEach((item) =>{
html1 += `<li>${item}</li>`;
})
html1 += `</ol>`;
let html2 = `<ol>`;
배열명.forEach(item => {
html2 += `<li>${item}</li>`;
})
html2 += `</ol>`;
let html3 = `<ol>`;
배열명.forEach(item => html2 += `<li>${item}</li>`)
html3 += `</ol>`;
정리
- 09_javascriptStandardObject
-> 01_Array_class -> 01_forOf_forEach_push_pop_splice_reverse_sort_slice_indexOf.html, 01.css, 01.js