admin管理员组文章数量:1356386
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
4 Answers
Reset to default 5i 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!");
}
}
本文标签: javascriptCheck if an array is empty or notPostmannodejsStack Overflow
版权声明:本文标题:javascript - Check if an array is empty or not - Postman - node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743944398a2566138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论