0

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,
            "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
        }
}]
sjgmamer
  • 73
  • 1
  • 6
  • Does this answer your question? [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – 0stone0 Feb 21 '22 at 10:42
  • iterate thought your "variant" object and check forEach object inside, if object.stock.available === true && object.stock.quantity > 3 – Chiquito_Sensei Feb 21 '22 at 10:45
  • You can use [`Object.entries`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) or a simple [`for .. in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop – derpirscher Feb 21 '22 at 10:49
  • @0stone0 but as `variants` is an object and not an array that doesn't seem to work here ... – derpirscher Feb 21 '22 at 10:50

1 Answers1

2

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"]
Dev-2019
  • 547
  • 3
  • 11
  • Above works perfectly for above use case but what needs to be done if I want to get `meta.ean` value as well? I have updated the original JSON to add meta object – sjgmamer Mar 15 '22 at 16:09
  • you can read like this ,`variants['194922428032']['meta']['ean']` what is the required output? – Dev-2019 Mar 15 '22 at 16:45
  • the required output is to extract ean value --> `609332822610` – sjgmamer Mar 15 '22 at 17:16
  • I don't think I can use `variants['194922428032']['meta']['ean']` because `194922428032` this is dynamic and varies on each request. @nithin-pb – sjgmamer Mar 15 '22 at 17:33
  • not sure this is the required solution `const eans = ids.reduce((acc, curr)=>{ const item = testArray[0].variants[curr] if(item?.meta?.ean) acc.push(item.meta.ean) return acc }, [])` this is an extension to the original answer – Dev-2019 Mar 16 '22 at 12:17