要学会用 arr != null 来判断一个对象或者数组是否为空

有时候在项目代码里边数组或者对象还没有那么快拿到数据,所以就要先判断它是否为空,否则会访问不到数组的元素或对象里边的属性,同时还会报undefined之类的错误。

if ( arr != null ) {

  console.log( arr[0].status )

}

posted @ 2023-03-03 19:08  scofield1906  阅读(165)  评论(0编辑  收藏  举报