arrays - How to reduce a list of multiple items to a key value pair object in Javascript? -


I have an array of states:

  ['CO', 'ca' 'CO', 'AL', ..., 'NV']   

And I would like to reduce it:

  {'CO': 9 , 'CA': 17, 'AL': 1, etc.}   

The value is always in the array.

What is the most effective way to do this?

  function compress2dict (raw_arr) {var ret = {}; (Var i = 0; i & lt; raw_arr.length; i ++) for {var item = raw_arr [i]; Retired [items] | = 0; Retired [item] ++; } Return return; } A = ['CO', 'BO', 'CO', 'CC', 'CC', 'CO', 'CC'] B = Compressor 2D (A) B {'BO': 1, 'CC '3,' CO ': 3}    

Comments