使用背景
在配合后端post请求接口的时候 仅需要将有值的字段传入接口中
关键代码
javascript">cleanDataObj(obj) {Object.keys(obj).forEach((key) => {if (obj[key] === '') {delete obj[key]}})},
demo如下
javascript">export default {data() {return {demoObject:{name:'小花',sex:'',hobby:'爬山',}}},methods:{cleanDataObj(obj) {Object.keys(obj).forEach((key) => {if (obj[key] === '') {delete obj[key]}})},demoHandle(){let needObject = this.cleanDataObj(this.demoObject)console.log(needObject)// 结果为 {name:'小花',hobby:'爬山'}}}}
希望对你们有所帮助