比较两个数组并验证主数组是否有子数组的值。

问题描述 投票:0回答:1

我有两个数组,我想验证一个数组中的数据是否存在于另一个数组中。

[
    {
        "productDisplay": "ZXP 105",
        "productNumber": "WZDR 112"
    },
    {
        "productDisplay": "ZXP 106",
        "productNumber": "WZDR 113"
    }
]

儿童数组

[{productDisplay:"ZXP 105",
productNumber:"WZDR 112"}]

我正在验证的代码

expect(MasterArray).to.deep.include(ChildArray)

expect(MasterArray).to.deep.include(ChildArray) UnhandledPromiseRejectionWarning: AssertionError: expected [ Array(2) ] to deep include [ { productDisplay: 'ZXP 105', productNumber: 'WZDR 112'})]!

如何执行这种情况下的测试案例?

javascript node.js mocha chai
1个回答
0
投票
var master = [{
        "productDisplay": "ZXP 105",
        "productNumber": "WZDR 112"
    },
    {
        "productDisplay": "ZXP 106",
        "productNumber": "WZDR 113"
    }
];
var child = [{ProductDisplay:"ZXP 105",
ProductNumber:"WZDR 112"}];

var res = master.includes((product,index) => product.productNumber == child[index].ProductNumber);
console.log(res); 

0
投票

let arrayA = [5,6,8];
let arrayB = [5,6,8];

let sameArrays = (arrayA.length == arrayB.length) && arrayA.every(function(element, index) {
    return element === arrayB[index]; 
});

document.write('Both are arrays are the same: ' + sameArrays);
© www.soinside.com 2019 - 2024. All rights reserved.