Vue에서 객체 배열 순회하기
이제 객체 배열을 순회하는 방법을 배워봅시다. 예제를 통해 살펴보겠습니다. 다음과 같은 배열이 있다고 가정해 보겠습니다:
data() {
return {
users: [
{
name: 'name1',
surn: 'surn1',
},
{
name: 'name2',
surn: 'surn2',
},
{
name: 'name3',
surn: 'surn3',
},
]
}
}
반복문으로 배열을 순회하면서 각 사용자에 대해 별도의 단락에 이름과 성을 출력해 봅시다:
<template>
<p v-for="user in users">
{{ user.name }}
{{ user.surn }}
</p>
</template>
다음 배열이 주어졌습니다:
data() {
return {
hrefs: [
{href: '1.html', text: 'text1'},
{href: '2.html', text: 'text2'},
{href: '3.html', text: 'text3'},
]
}
}
반복문을 사용하여 다음 코드를 생성하세요:
<ul>
<li><a href="1.html">text1</a></li>
<li><a href="2.html">text2</a></li>
<li><a href="3.html">text3</a></li>
</ul>
다음 제품 배열이 주어졌습니다:
data() {
return {
products: [
{
name: 'product1',
price: 100,
quantity: 5
},
{
name: 'product2',
price: 200,
quantity: 4
},
{
name: 'product3',
price: 300,
quantity: 3
},
]
}
}
반복문을 사용하여 다음 코드를 생성하세요:
<table>
<tr>
<td>product1</td>
<td>100</td>
<td>5</td>
</tr>
<tr>
<td>product2</td>
<td>200</td>
<td>4</td>
</tr>
<tr>
<td>product3</td>
<td>300</td>
<td>3</td>
</tr>
</table>