简体   繁体   English

检查数组是否为空 - Postman - node.js

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

I am getting the response in this format:我收到这种格式的回复:

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

After sending a request, this is the response i get.发送请求后,这是我得到的响应。

And i want to check f.我想检查 f。 ex.前任。 does test1 is empty or not. test1是否为空。

And if it is empty it's enough to just console log.如果它是空的,只需控制台日志就足够了。

i think you are looking for this.我想你正在寻找这个。 using .length property of the array使用数组的.length属性

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

use javascript array.length property.使用 javascript array.length 属性。

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

  {

you should run something like this你应该运行这样的东西

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