我如今從後端獲取了一個數組,數組裏面有n個對象,對象裏面又有n個數組,固然裏面也有對象,可能描述有點混亂,下面有代碼能夠一目瞭然,我如今要作的是獲取數組裏的數組對象的全部個數,而後再得到對象裏name爲a的個數,我首先想到的是for循環和便利函數,這兩種方法都能便利出每一個數組裏的對象個數,可是怎麼所有加起來呢?而後我試了累加的方法實現不了,也多是個人技術不行,後來我就問了一下公司的前端同事,短短 的幾行代碼就達到了我要的目的,實在是佩服,感受後期確定能用到,因此在這裏整理出來,畢竟本身技不如人。。好了,上代碼。前端
<template>
<div class="hello">後端
<p>name爲a的個數:{{medalStatus.nameLength}}</p> <p>總個數:{{medalStatus.total}}</p>
</div>
</template>數組
<script>
export default {函數
name: "HelloWorld", data () { return { lists: '' } }, methods: { getData () { var listArry = [ { typeList: [ { id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" } ], type: 1 }, { typeList: [ { id: 2, name: "b" }, { id: 3, name: "a" } ], type: 2 }, { typeList: [ { id: 3, name: "c" } ], type: 3 }, { typeList: [ { id: 4, name: "d" } ], type: 4 }, { typeList: [ { id: 5, name: "a" } ], type: 5 } ] this.lists = listArry } }, computed: { medalStatus () { console.log(this.lists) let total = 0 let nameLength = 0 this.lists.forEach(dict => { let nameList = dict.typeList.filter(item => item.name === "a") nameLength = nameLength + nameList.length total = total + dict.typeList.length }) return { nameLength, total } } }, created: function () { this.getData() }
}
</script>this