Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am writing tests in postman and I would like to extract the list of Ids inside variants object matching condition where stock.available ==true and quantity > 3. However my issues lies that number inside variants object is random value which is 194922428018, 194922428025 and 194922428032. Final output should results only 194922428025 and 194922428032 which are the items with stock. Thanks in Advance.

[{
    "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
        }
}]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Hope this would work for you

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"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading