- 刚才采用foreach来遍历数组,达到格式化el-table中的数据,发现用foreach不生效,但是用户for循环没问题。
吓我一跳,赶紧看了一下js中的foreach用法,没毛病啊。用了这么多年难道还会用错。
<!--
* @Descripttion: js中的foreach用法
* @version:
* @Author: zhangfan
* @email: 2207044692@qq.com
* @Date: 2020-07-03 09:10:28
* @LastEditors: zhangfan
* @LastEditTime: 2020-07-22 16:46:29
-->
<template>
<div class="tableBox">
<el-button type="primary" @click="traverseArray">遍历数组</el-button>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{
name: "王小虎",
address: "027",
sex: 1
},
{
name: "王二狗",
address: "0851",
sex: 1
},
{
name: "王小丫",
address: "024",
sex: 0
},
{
name: "王小翠",
address: "0451",
sex: 0
}
]
};
},
methods: {
/**
* @description: forEach遍历数组
*/
traverseArray() {
// this.tableData.forEach(function(item) {
// console.log(item);
// });
//采用箭头函数方式
// this.tableData.forEach(item => {
// console.log(item);
// });
// forEach方法中的function回调有三个参数
this.tableData.forEach((item, index, array) => {
console.log(item.name);
console.log(index);
console.log(array);
});
}
}
};
</script>
<style scoped lang="less">
</style>
就当作练练手吧。