Javascript 如何在数组中断言数组?

Javascript 如何在数组中断言数组?,javascript,json,testing,postman,assertion,Javascript,Json,Testing,Postman,Assertion,有人能帮我在邮递员的数组中断言数组吗 我有这样一种代码的和平: "documents": [ { "fileName": "file01_guid.pdf", "documentType": "document", "parentFiles": [ "

有人能帮我在邮递员的数组中断言数组吗

我有这样一种代码的和平:

"documents": [
        {
            "fileName": "file01_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        },
        {
            "fileName": "file02_guid.pdf",
            "documentType": "document",
            "parentFiles": [
                "file01.pdf"
            ]
        }
我需要使用以下方法断言“ParentFiles”数组:

var array = [];
var range = (json_response.data.documents).length

for (var i = 0; i < range; i++)

{
 var file = json_response.data.documents[i].fileName
 var type = json_response.data.documents[i].documentType;

 array.push(file)
 array.push(type)
}
提前感谢您

您可以通过简单地筛选和检查长度来验证
“file01.pdf”
是否存在。您还可以通过降低成本来更有效地构建
阵列

const json\u响应={
“数据”:{
“文件”:[{
“文件名”:“file01\u guid.pdf”,
“文档类型”:“文档”,
“父文件”:[“file01.pdf”]
}, {
“文件名”:“file02_guid.pdf”,
“文档类型”:“文档”,
“父文件”:[“file01.pdf”]
}]
}
};
const array=json_response.data.documents.reduce((acc,doc)=>{
const{fileName,documentType,parentFiles:[pfName]}=doc;
返回[…acc,文件名,文档类型,pfName];
}, []);
console.log(数组);
//“file01.pdf”正好存在两个实例。
console.log(array.filter(val=>val==“file01.pdf”).length==2)

。作为控制台包装{top:0;最大高度:100%!important;}
您可以使用foreach循环并比较值

//store expected files in order
array=["file01.pdf","file01.pdf"]

//now forEach on each element
json_response.data.documents.forEach( (elem,index,arr)=>{
pm.expect(elem.parentFiles[0]).to.be.eql(array[index])
})
如果您只想比较两个数组,那么:

pm.expect(_.isEqual(array,array2)).to.be.true

lodash isEqual可用于比较两个类似对象的数组

要断言什么?为什么还要pushind文档类型?我想使用“include”函数断言parentFiles数组,就像我在文章中描述的那样。我有点困了你想断言每个对象的parentFile数组或者你想用一些数组断言新数组如果有两种方法可以达到我的目标-我想知道这两种方法。为什么你不能只使用lodash?@PDHide,因为JavaScript已经进化,依赖项是额外的开销。Ops使用的是postman而不是browser@PDHide这个代码也可以在NodeJS中工作,它不是DOM操作(或依赖)代码。@Mr.polywhill最后我决定使用您的方法:
const array=json_response.data.documents.reduce((acc,doc)=>{const{fileName,documentType,parentFiles:[pfName]}=doc;return[…acc,fileName,documentType,pfName];},[])因为我需要响应,所以文档可以不同。我研究过你的方法,它完全符合我的需要。谢谢,我还不明白你的“循环”选项。当我尝试时:
pm.test('parentFiles等于“BR_21080_file18.pdf”,function(){array=[“file01.pdf”,“file01.pdf”]pm.response.json().documents.forEach((elem,index,arr)=>{pm.expect(elem.parentFiles[0]).to.be.eql(array[index]))我收到了“parentFiles equal”file18.pdf“| TypeError:无法读取未定义的属性'forEach'”是更新的代码json_response.data.documents
pm.expect(_.isEqual(array,array2)).to.be.true