最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if an array is empty or not - Postman - node.js - Stack Overflow

programmeradmin8浏览0评论

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 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.

Share Improve this question asked Sep 12, 2017 at 9:05 MartOMartO 411 gold badge1 silver badge4 bronze badges 2
  • 1 Check for the length of the array.. – Subburaj Commented Sep 12, 2017 at 9:06
  • pm.expect(yourJsonData.test1.length).to.eql(0); – Malik Khalil Ahmad Commented Jul 7, 2023 at 10:00
Add a ment  | 

4 Answers 4

Reset to default 5

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!");
   }
}
发布评论

评论列表(0)

  1. 暂无评论