学无先后,达者为师

网站首页 Vue 正文

vue 判断数据是否为空

作者:deardanyang 更新时间: 2022-02-17 Vue

 vue判断对象是否为空、判断数组是否为空

<template>
  <div>数据是否为空</div>
</template>

<script>
export default {
  name: "Orders",
  data() {
    return {
      abj: {},
      arr: []
    };
  },
  created() {
    //判读数组是否为空
    if(this.arr == undefined || this.arr.length <= 0) {
      console.log("数组为空");
    }
    if(this.arr !== undefined && this.arr.length > 0) {
      console.log("数组不为空");
    }

    //判断对象是否为空
    if(JSON.stringify(this.obj) === "{}"){
      console.log("对象为空");
    } else {
      console.log("对象不为空");
    }
  },
  methods: {},
};
</script>

<style lang="scss" scoped></style>

1.数组为空

this.arr == undefined || this.arr.length <= 0

2.数组不为空

this.arr !== undefined && this.arr.length > 0

3.对象判断

JSON.stringify(this.obj) === "{}"

 

原文链接:https://blog.csdn.net/qq_34707272/article/details/106961741

栏目分类
最近更新