To calculate the number of repetitions by map

Source: Internet
Author: User

Originally in a bit confused situation, may be that period of time to write JS write dizzy, almost put him is the customer service side code to forget. The teacher's word mvc is as good as bluntness, wake me up from a dream, say no more, first give the slag code before

     //Calculate Total Price       var cnt= 0;       $ (". Price"). each (function () {        cnt + =parseint (This). text ());       });       $ ("#count "). Text (CNT);             //Create array        var a=new array ();      var i=0;      $ (". MenuId"). each (function () {      a[i]=$ (this). attr ("Sid");        i++;      });          //  Statistics Code       b = new array ();       for  (Var index in a)  {         if  (B[a[index]] == null)           b[a[index]]  = 1;        else b[a[index]]++;       }      //  Output Statistics       for   (VAR INDEX IN B)  {        console.info (index );         $ ("." +index). Slice (1). Remove ();         $ (". Copy_" +index). Text (B[index]);       }

Do not pay attention to see, these code is one of my lessons, this in the case of DOM is generated by the DOM processing is obviously very unwise, can not be said to negate, but there is a better solution (and just relative)

Processing data in the backend only needs to be as simple as:

map<restaurantmenu,integer> Restaurantmenumap = new linkedhashmap<restaurantmenu,integer> (); for (Orderinguser orderinguser:orderingusers) {Integer count= restaurantmenumap.get (orderinguser.getrestaurantme           Nu ());            Restaurantmenumap.put (Orderinguser.getrestaurantmenu (), (count = = null)? 1:count + 1);        Countprice+=orderinguser.getrestaurantmenu (). GetPrice (); }

Restaurantmenumap is the result you want to get. Coutprice is the sum of calculations.

Eat a pad your wit, key value can be stored object, I am not the first day to know, but it is the first time to use. How do you feel about the students after reading them? Welcome to share your criticism!

To calculate the number of repetitions by map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.