I need help in iterating through arrays in Vue.
The data structure - coming from the database - is a list of vegetables, with each case identified according to its genus and common name and an ID that is specific for the genus.
[{"genus":"Allium","name":"Garlic","id":19},
{"genus":"Allium","name":"Leek","id":19},
{"genus":"Allium","name":"Onions","id":19},
{"genus":"Allium","name":"Shallot","id":19},
{"genus":"Allium","name":"Spring onions","id":19},
{"genus":"Brassica","name":"Broccoli","id":20},
{"genus":"Brassica","name":"Cabbage","id":20},
{"genus":"Brassica","name":"Cauliflower","id":20},
{"genus":"Brassica","name":"Kale","id":20},
{"genus":"Cucurbit","name":"Bittermelon","id":13},
{"genus":"Cucurbit","name":"Cucumber","id":13},
{"genus":"Cucurbit","name":"Pumpkin","id":13},
{"genus":"Cucurbit","name":"Rockmelon","id":13},
{"genus":"Cucurbit","name":"Squash","id":13},
{"genus":"Cucurbit","name":"Zucchini","id":13},
{"genus":"Legume","name":"Beans","id":8},
{"genus":"Legume","name":"Broad beans","id":8},
{"genus":"Legume","name":"Peas","id":8}]
I identify the unique genus values in the data (see "uniqueGenera" below) and then loop through all the vegetables that belong to that genus and show their name.
I have two problems.
First, once I've identified the unique genus names, I can't seem to access the ID associated with that genus (in "navigateTo(item.id))
<v-card
v-for="(item,i) in uniqueGenera(genus)"
:key="i"
ripple
@click="navigateTo(item.id)" class="hand"
>
<v-layout fill-height>
<v-flex xs12 align-end flexbox>
<p v-text="item"></p>
<span
v-for="(card, index) in cards"
v-if = "card.genus === item"
>
{{card.name + ', '}}
</span>
</v-flex>
</v-layout>
</v-card>
Second, I want to display the vegetable names within each genus as a comma-separated list, but I don't know how to work out when we've reached the last item of the current array for any given genus in order to stop the comma being added to that last name.
I'm identifying the unique genera as follows:
uniqueGenera: function () {
var vm = this;
return function (keyname) {
var output = [];
var keys = [];
vm.cards.forEach(function (card) {
var key = card[keyname];
if (keys.indexOf(key) === -1) {
keys.push(key);
output.push(card[keyname]);
}
});
return output;
};
},
cardsdata property? What's up with<p v-text="item"></p>? Isn'titeman object?