I've been trying for 3 days straight to figure this out, by this point I don't even know what to search for.
I have an array of objects, I would like to get the greatest "bid_amount" for each "item_id", and then use the item_id, bid_amount and bidder_id in the DOM. I am stuck at the first step.
[
{
"item_id": "1",
"bid_amount": "765432",
"bidder_id": "298709"
},
{
"item_id": "1",
"bid_amount": "380",
"bidder_id": "606396"
},
{
"item_id": "2",
"bid_amount": "545",
"bidder_id": "606396"
},
{
"item_id": "2",
"bid_amount": "525",
"bidder_id": "317740"
},
{
"item_id": "2",
"bid_amount": "505",
"bidder_id": "606396"
},]
function getBids() {
var request = new XMLHttpRequest();
request.open('POST', '/get_bids.php', true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
request.onload = function() {
bids = JSON.parse(this.response)
bids.reduce(function(prev, curr) {
currItemId = curr.item_id
currBidAmnt = curr.bid_amount
const item_id = (prev[curr.item_id] || []);
const prevBidsForId = prev[currItemId];
if (prevBidsForId) {
console.log(' a bid for this item exists, now i need to check if the current bid amount is greater than the previous bid amount for this item ')
item_id.push(curr)
prev[currItemId] = item_id
} else {
// push current item to prev
console.log(' a bid for this item does not exist ')
item_id.push(curr)
prev[currItemId] = item_id
}
return prev
}, [])
}