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) === "{}"

 

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐