I am new to vue and I was trying to sort my array by the first name. I have a button that should sort the player name by the first name. But when I click on the button it throws an error like this.todos.player_name.sort() is not a function. Here is my code on
JsFiddle = https://jsfiddle.net/ujjumaki/r6wp38yj/77/
View
<div id="app">
<table>
<tr>
<th><b>First Name </b></th>
<th><b> Last Name </b> </th>
<th><b> ID </b> </th>
</tr>
<tr v-for="fetchName in todos.player_name">
<td>{{fetchName.first_name}}</td>
<td>{{fetchName.last_name}}</td>
<td>{{fetchName.id}}</td>
</tr>
</table>
<br>
<button v-on:click=alertName()>Sort by first name</button>
</div>
Methods
new Vue({
el: "#app",
data(){
return{
todos: {
mission:1,
player_name:[
{
first_name: "Harry", last_name: "Kui" , id:'103',
},
{
first_name: "Ali", last_name: "Kui", id:'107',
},
{
first_name: "Bee", last_name: "Hui", id:'99',
}
]
}
}
},
methods: {
alertName: function(){
this.todos.player_name.sort(); /** sort function is throwing an error here **/
console.log(this.todos.player_name); /** and this does not format array objects followed by first name **/
}
}
})