Reputation: 19
i'm kinda new into express and nodejs, i had these nested json object data and i wanna get only single Max value from price element, inside these nested object.
"variants": [
{
"uuid": "8a310a90-fc16-11ec-a7c2-f184ed5b25fd",
"name": "Varian 1",
"variant_details": [
{
"uuid": "8a323690-fc16-11ec-95d2-abf7a96fd2d7",
"name": "B",
"pricing_and_stock": [
{
"price": 12000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a332b50-fc16-11ec-ad78-0b0b2937dadc",
"name": "A",
"pricing_and_stock": [
{
"price": 11000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a33dfe0-fc16-11ec-966f-fff68c8324f9",
"name": "C",
"pricing_and_stock": [
{
"price": 13000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a34c960-fc16-11ec-a3b3-554cbc851abe",
"name": "D",
"pricing_and_stock": [
{
"price": 14000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a358d80-fc16-11ec-a780-ad8ebd937a76",
"name": "E",
"pricing_and_stock": [
{
"price": 15000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a3940b0-fc16-11ec-b49a-b74378d9b902",
"name": "F",
"pricing_and_stock": [
{
"price": 16000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a3a0ed0-fc16-11ec-a3a8-4f16f339644a",
"name": "G",
"pricing_and_stock": [
{
"price": 17000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a3ae3c0-fc16-11ec-b5be-25eaa38cccf3",
"name": "H",
"pricing_and_stock": [
{
"price": 18000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a3c1150-fc16-11ec-ba75-d7375bdbe724",
"name": "I",
"pricing_and_stock": [
{
"price": 19000,
"stock": 0,
"qty": 0
}
]
},
{
"uuid": "8a3ca6f0-fc16-11ec-8398-d72cccfc72bb",
"name": "J",
"pricing_and_stock": [
{
"price": 20000,
"stock": 0,
"qty": 0
}
]
}
]
}
]
}
i've already tried several trick such as mapping manually the object, flatten the object into arrays, and some other thing but not all those trick work. can anybody knew the best way to do that? really much appreciated !
Upvotes: 0
Views: 82
Reputation: 16676
The maximum can be determined with a nested loop written in Javascript:
var maxprice = 0;
for (var v of data.variants)
for (var d of v.variant_details)
for (var p of d.pricing_and_stock)
if (p.price > maxprice)
maxprice = p.price;
But if the data ultimately come from a database, it is a waste of resources to transfer all data to Javascript and determine the maximum in this way. It would be more efficient to use database operations like max
to determine the maximum price on the database and only transfer this single value to Javascript.
Upvotes: 1