안녕하세요. 데이터바인딩 관련해서 질문이 있습니다..!
저는 아래와 같이 코드를 짜서 화면에 데이터를 불러오는 것까지 성공을 했는데요..!
for 반복문을 사용해서 코드를 좀 줄여보고 싶은데 도저히 방법을 몰라 질문드립니다 ㅠㅠ
어떤식으로 코드를 짤 수 있을까요..?
<script>
var products = [
{ id : 0, price : 70000, title : 'Blossom Dress' },
{ id : 1, price : 50000, title : 'Springfield Shirt' },
{ id : 2, price : 60000, title : 'Black Monastery' }
];
const priceis = '가격 :'
// 데이터 바인딩 퀴즈!
// 1번 상품
document.querySelectorAll('.card-body h5')[0].innerHTML = products[0].title
document.querySelectorAll('.card-body P')[0].innerHTML = `${priceis}` + products[0].price
// 2번 상품
document.querySelectorAll('.card-body h5')[1].innerHTML = products[1].title
document.querySelectorAll('.card-body P')[1].innerHTML = `${priceis}` + products[1].price
// 3번 상품
document.querySelectorAll('.card-body h5')[2].innerHTML = products[2].title
document.querySelectorAll('.card-body P')[2].innerHTML = `${priceis}` + products[2].price
</script>