Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查包含多个属性的支票?_Javascript_Node.js_Mocha.js_Chai - Fatal编程技术网

Javascript 如何检查包含多个属性的支票?

Javascript 如何检查包含多个属性的支票?,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我希望在我的Node.js应用程序的mocha测试中检查数组是否包含对象,我知道我可以: 或 但是有没有一种方法可以在不知道所有属性的情况下检查是否包含多个属性 例如,如果我有一个数组: var arr = [{ pet: 'cat', owner:1, id:1 }, { pet: 'cat', owner:2, id:2 },{ pet: 'dog', owner:1, id:3 }] 我想检查一个包含{pet:'cat',owner:1}的对象,我不知道测试前的id是什么,因为这是随机生

我希望在我的Node.js应用程序的mocha测试中检查数组是否包含对象,我知道我可以:

但是有没有一种方法可以在不知道所有属性的情况下检查是否包含多个属性 例如,如果我有一个数组:

var arr = [{ pet: 'cat', owner:1, id:1 }, { pet: 'cat', owner:2, id:2 },{ pet: 'dog', owner:1, id:3 }]
我想检查一个包含{pet:'cat',owner:1}的对象,我不知道测试前的id是什么,因为这是随机生成的


我如何才能以高效的方式做到这一点,最好使用chai、chai插件或vanilla js,因为我不能使用其他框架。我查看了,但我认为这对我的理解没有帮助

据我所知,您希望过滤对象的javascript数组。您可以使用jQueryAPI grep,如下所示

filter = $.grep(arr, function (v) {
        return v.pet === "cat" && v.owner === 1;
});

希望它能有所帮助……

抱歉,也许我不清楚这个问题,但这是服务器端javascript,我也说过我不能使用其他框架,但很高兴知道它存在。您只需添加两条语句即可获得所需的逻辑?arr.should.contain.a.thing.with.property'pet'、'cat'。。。arr.should.contain.a.thing.with.property'owner',1;我会在github上提交一张关于柴的事情的罚单,看看他们怎么说。
var arr = [{ pet: 'cat', owner:1, id:1 }, { pet: 'cat', owner:2, id:2 },{ pet: 'dog', owner:1, id:3 }]
filter = $.grep(arr, function (v) {
        return v.pet === "cat" && v.owner === 1;
});