Group like values in Javascript

I have a script in which I select a primary location from a drop down box.  On change, an ajax called is made to a PHP file which returns an array of items, products, and the inventory level for that each of those.

The javascript array looks like this:

{"item_inventory":[{"item_id":133,"product_id":1,"display_id":1,"inventory_level":"4"},{"item_id":133
,"product_id":1,"display_id":1,"inventory_level":"4"},{"item_id":180,"product_id":0,"display_id":1,"inventory_level"
:0},{"item_id":180,"product_id":0,"display_id":1,"inventory_level":0},{"item_id":188,"product_id":0,"display_id"
:1,"inventory_level":1},{"item_id":188,"product_id":0,"display_id":1,"inventory_level":1},{"item_id"
:192,"product_id":0,"display_id":1,"inventory_level":1},{"item_id":192,"product_id":0,"display_id":1
,"inventory_level":1}]}

Open in new window


What I'm hoping to do is loop through each of those results and have it complete the following logic:

If the product_id equals 0, group by the item_id, otherwise, group by the product_id.

This grouping should essentially tell me the total inventory level for each item or product that was returned.

I then have an HTML page which I will be passing these values back to.

Say my page has an html table that looks like this:

Product 1
Product 1

Item 180

Item 192
Item 192

If the inventory grouping returned in javascript equals the total number of products ids on the page, it would return a "OK" value to the html page.  If There are more items on the page than what is available in inventory, it would return a "NO" value to the html page.

I guess the first part of this would be getting the items to group so I know exactly what is in inventory for each product/item.

I'm sure I'll need to pass a "total_requested" value back with the initial array to compare the values to see if we have enough in stock for the warehouse selected.
LVL 1
t3chguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
<script type="text/javascript">
var data={
    "item_inventory": [{
        "item_id": 133,
        "product_id": 1,
        "display_id": 1,
        "inventory_level": "4"
    }, {
        "item_id": 133,
        "product_id": 1,
        "display_id": 1,
        "inventory_level": "4"
    }, {
        "item_id": 180,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 0
    }, {
        "item_id": 180,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 0
    }, {
        "item_id": 188,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 1
    }, {
        "item_id": 188,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 1
    }, {
        "item_id": 192,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 1
    }, {
        "item_id": 192,
        "product_id": 0,
        "display_id": 1,
        "inventory_level": 1
    }]
};


var result={};
for(var i=0, limit = data.item_inventory.length; i < limit; ++i)
{
	var key = data.item_inventory[i].item_id;
	if( typeof(result[key])=='undefined' )
	{
		result[key]=[];
	}
	delete(data.item_inventory[i].item_id);
	result[key].push(data.item_inventory[i]);
}

document.write(JSON.stringify(result));
</script>

Open in new window


Result:
{
	"133": [{
		"product_id": 1,
		"display_id": 1,
		"inventory_level": "4"
		},
		 {
		"product_id": 1,
		"display_id": 1,
		"inventory_level": "4"
	}],

	"180": [{
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 0
		},
		 {
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 0
	}],

	"188": [{
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 1
		},
		 {
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 1
	}],

	"192": [{
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 1
		},
		 {
		"product_id": 0,
		"display_id": 1,
		"inventory_level": 1
	}]
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.