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
}
}]
CodePudding user response:
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"]