javascript - Getting unique value from the json data -
how unique merchant name , corresponding avgprice , numproducts using jquery? i`ve been having hard time seperating json data. please me guys.. parsing json file using $.getjson. creating graph need use seperator after every merchant names. here avgprice , numproducts x , y axes values. if use this
$.getjson("mock/insight72.json", function(returneddata) { datalength = returneddata.data.length; console.log(returneddata) response = returneddata; var x = new array(); var y = new array(); var mytext, f; ( = 0; < datalength; i++) { x[i] = returneddata.data[i].avgprice; y[i] = returneddata.data[i].numproducts; x , y axis getting stored in same order. need parse should take avgprice , numproducts corresponding merchant name. hope clarifies..
{ "id" : "72", "title" : "item category product level average price comparison", "xlabel" : null, "ylabel" : "average price", "zlabel" : null, "data" : [ { "avgprice" : 10, "categoryid" : "152b7934dd7e4fbeac56f825b5deaebd", "categoryname" : "coffee makers", "merchantid" : "99a8cd5687d245f8bff2152fec710973", "merchantname" : "crate & barrel", "numproducts" : 400 }, { "avgprice" : 20, "categoryid" : "152b7934dd7e4fbeac56f825b5deaebd", "categoryname" : "coffee makers", "merchantid" : "f5b2c736eace488a859fb6c56f366522", "merchantname" : "williams-sonoma", "numproducts" : 500 }, { "avgprice" :30, "categoryid" : "152b7934dd7e4fbeac56f825b5deaebd", "categoryname" : "coffee makers", "merchantid" : "6ee163f4f236466289fae97bb40351b7", "merchantname" : "amazon", "numproducts" : 38 }, { "avgprice" : 40, "categoryid" : "50215adc9ef64f91b4f4898fda7cf0b5", "categoryname" : "dishwashers", "merchantid" : "99a8cd5687d245f8bff2152fec710973", "merchantname" : "crate & barrel", "numproducts" : 300 }, { "avgprice" : 50, "categoryid" : "50215adc9ef64f91b4f4898fda7cf0b5", "categoryname" : "dishwashers", "merchantid" : "f5b2c736eace488a859fb6c56f366522", "merchantname" : "williams-sonoma", "numproducts" : 320 }, { "avgprice" : 60, "categoryid" : "50215adc9ef64f91b4f4898fda7cf0b5", "categoryname" : "dishwashers", "merchantid" : "6ee163f4f236466289fae97bb40351b7", "merchantname" : "amazon", "numproducts" : 350 } ] }
here's working fiddle: http://jsfiddle.net/esv6f/
/* items merchant*/ function get_items_by_merchant(merchant_name) { var items = new array(); $.each(json.data, function(index, item) { if (item.merchantname == merchant_name) items.push(item); }); return items; } /* items of merchant "amazon" */ var amazon_items = get_items_by_merchant("amazon"); /* count total num of products */ var amazon_num_products_total = 0; /* loop items */ $.each(amazon_items, function(index, item) { amazon_num_products_total += item.numproducts; // add numproducts /* alert avgprice , numproducts of current item */ alert("avgprice: " + item.avgprice + " numproducts: " + item.numproducts); }); /* alert total num of products of amazon */ alert("amazon numproducts total: " + amazon_num_products_total);
Comments
Post a Comment