断言一个 JSON 数组包含一个 属性

Assert that a Array of JSONs contains one property

我有一个允许 add/delete/edit 设备的设备商店项目,我正在尝试在邮递员中进行测试,在 POST 之后,将设备添加到列表中,该设备可以是在我的回复中发现 body.I 正在使用 Postman BDD 和 Chai sintax

[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}

]

这是我的回复,可以看出它是 return 一个 JSON 数组,我正在寻找一种优雅的方式来断言具有 属性 [=18= 的设备] 在数组中。我对可以帮助我实现这一目标的任何其他 javascript 库持开放态度。

您可以使用 Array#some:

response.some( o => o.name == "Pixel 2" )

结果为布尔值。

不需要图书馆。您可以使用 Array.prototype.some:

if (response.some(i => i.name == 'Pixel 2')) {
  // ...
}

您可以使用 find 方法接受 回调 提供的函数。

var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var item=array.find(function(item){
  return item.name=="Pixel 2";
});
console.log(item);
console.log("Exists: "+item!=undefined);

另一种方法是使用includes函数。

var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var exists=array.map(function(item){
  return item.name;
}).includes("Pixel 2");
console.log(exists);

使用 Array#filter with Array#length 查找您的项目是否存在

const array = [{
        "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
        "name": "Huawei",
        "alias": "electronics",
        "quantity": 10,
        "price": 200,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
        "name": "Pixel 2",
        "alias": "electronics",
        "quantity": 10,
        "price": 300,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
        "name": "SONYk",
        "alias": "electronicsm",
        "quantity": 122,
        "price": 2222,
        "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
        "links": []
    },
    {
        "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
        "name": "ASUS1",
        "alias": "electronics",
        "quantity": 1222,
        "price": 2222,
        "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
        "links": []
    }
];

const hasPixel2 = array.filter(item => item.name === 'Pixel 2').length > 0;
console.log(hasPixel2);

Array#Filter

var array=[
{
    "deviceid": "5a72fec0-a220-4484-a058-e1e56dfc56c5",
    "name": "Huawei",
    "alias": "electronics",
    "quantity": 10,
    "price": 200,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "90807800-c66c-46ec-ae46-687464e62797",
    "name": "Pixel 2",
    "alias": "electronics",
    "quantity": 10,
    "price": 300,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "c2bd507d-e544-4ce8-b272-34cab732adb4",
    "name": "SONYk",
    "alias": "electronicsm",
    "quantity": 122,
    "price": 2222,
    "categoryid": "91a71dc0-4d40-4d20-a59d-bb30a348a7f2",
    "links": []
},
{
    "deviceid": "caa97d05-2672-48c5-8c56-e0da1298b20d",
    "name": "ASUS1",
    "alias": "electronics",
    "quantity": 1222,
    "price": 2222,
    "categoryid": "11535983-b9d0-4a0f-8d53-ca204446e0b8",
    "links": []
}];
var item = array.filter(item => item.name === "Pixel 2");
console.log(item);