Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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,
            "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
        }
}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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"]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!