简体   繁体   中英

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. ex. does test1 is empty or not.

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

 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.

  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!");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM