簡體   English   中英

檢查數組是否為空 - Postman - node.js

[英]Check if an array is empty or not - Postman - node.js

我收到這種格式的回復:

{
    "test1": [],
    "test2": [],
    "test3": [],
    "test4": null,
    "test5": []
}

發送請求后,這是我得到的響應。

我想檢查 f。 前任。 test1是否為空。

如果它是空的,只需控制台日志就足夠了。

我想你正在尋找這個。 使用數組的.length屬性

 let response = { "test1": [], "test2": [], "test3": [], "test4": null, "test5": [] } if(response){ if(response.test1 && response.test1.length == 0) { console.log("array empty"); } }

使用 javascript array.length 屬性。

  if(test1.length){
  //if array is not empty do something here
 }
 else{
       //array is empty..do something else

  {

你應該運行這樣的東西

if (response){
    if (response["test1"] && response["test1"].length>0){
        //do stuff
    }

    if (response["test2"] && response["test2"].length>0){
        //do stuff
    }
    ...
}
let response = 
{
 "test1": [],
 "test2": [],
 "test3": [],
 "test4": null,
 "test5": []
}

if(response){
   // in your case, test4 is null, so you need to check if null value too
   // try console.log(response['test4'] === response['test1']) // should be false
   if(response.testX.length && response.testX !== null)
   {
      console.log("You're awesome!");
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM