如何从 java 脚本中更改数组字段中提取 ID 列表?

how to extract list of ids from changing array field in java script?

我正在用 postman 编写测试,我想提取变体对象匹配条件中的 ID 列表,其中 stock.available ==truequantity > 3。然而,我的问题在于 variants 对象内的数字是随机值,即 194922428018、194922428025 和 194922428032。最终输出结果应该只有 194922428025194922428032,它们是有库存的项目。提前致谢。

[{
    "appIdentifier": "53443434w-813f-4b59-b58a-deaf76847845",
    "categoryIds": null,
    "id": "205148",
    "variants": {
        "194922428018": {
            "id": "194922428018",
            "filterAttributes": null,
            "meta": {
                    "ean": "609332822610"
                },
            "stock": {
                "available": false,
                "lowOnStock": true,
                "leadTime": null,
                "quantity": 0
            },
            "categoryIds": null
        },
        "194922428025": {
            "id": "194922428025",
            "filterAttributes": null,
            "stock": {
                "available": true,
                "lowOnStock": true,
                "leadTime": null,
                "quantity": 9
            },
            "categoryIds": null
        },
        "194922428032": {
            "id": "194922428032",
            "filterAttributes": null,
            "stock": {
                "available": true,
                "lowOnStock": false,
                "leadTime": null,
                "quantity": 33
            },
            "categoryIds": null
        }
}]

希望这对你有用

const testArray = [{
    "appIdentifier": "53443434w-813f-4b59-b58a-deaf76847845",
    "categoryIds": null,
    "id": "205148",
    "variants": {
        "194922428018": {
            "id": "194922428018",
            "filterAttributes": null,
            "stock": {
                "available": false,
                "lowOnStock": true,
                "leadTime": null,
                "quantity": 0
            },
            "categoryIds": null
        },
        "194922428025": {
            "id": "194922428025",
            "filterAttributes": null,
            "stock": {
                "available": true,
                "lowOnStock": true,
                "leadTime": null,
                "quantity": 9
            },
            "categoryIds": null
        },
        "194922428032": {
            "id": "194922428032",
            "filterAttributes": null,
            "stock": {
                "available": true,
                "lowOnStock": false,
                "leadTime": null,
                "quantity": 33
            },
            "categoryIds": null
        }
}}
]

const ids = Object.entries(testArray[0]['variants']).reduce((acc, [key, value])=>{
    if(value.stock.available && value.stock.quantity > 3) acc.push(key)
  return acc
}, [])

console.log(ids) // ["194922428025", "194922428032"]