Skip to content

Instantly share code, notes, and snippets.

@abdullahnaseer
Created January 2, 2022 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahnaseer/7feb4cb557cef911cb15e81ec5773740 to your computer and use it in GitHub Desktop.
Save abdullahnaseer/7feb4cb557cef911cb15e81ec5773740 to your computer and use it in GitHub Desktop.
Vue Test - Computed Property
<template>
<div class="widget">
<div v-if="activeItems && activeItems.length > 0">
<ul>
<li
v-for="item in activeItems"
:key="item.id"
>
{{ item.name }}
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loading: true,
list: {
'John': true,
'Jane': true,
'Bob': false
},
}
},
computed: {
activeItems() {
/*
return `this.list` as an Array of object and filter by if active is true
Expected output
[
{name: 'John', active: true},
{name: 'Jane', active: true}
]
*/
return Object.keys(this.list).map((key) => {
return {
name: key,
active: this.list[key],
}
}).filter((item) => item.active);
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment