jquery - Counting duplicate objects in array of Javascript objects -


i'm trying parse sub-objects of javascript object count number of sub-objects same , return new json object counts. that's not clear, here's fiddle.

given crslistdata in fiddle, i'd return this:

objtoreturn = [             {                 key: "consigned",                 values:                     [                         { mapkey: 2007 (4), mapval: [["ford f-150 (3)"], ["honda civic (1)"]] },                         { mapkey: 2011 (1), mapval: "toyota camry (1)" },                         { mapkey: 2005 (1), mapval: "dodge ram (1)" }                     ]             },             {                 key: "run",                 values:                     [                         { mapkey: 2007 (2), mapval: "ford f-150 (2)" },                         { mapkey: 2011 (1), mapval: "toyota camry (1)" }                     ]             },             {                 key: "sold",                 values:                     [                         { mapkey: 2007 (1), mapval: "ford f-150 (1)" },                         { mapkey: 2011 (1), mapval: "toyota camry (1)" }                     ]             }         ]; 

if you'll notice in fiddle, i've commented on part mapkey , mapvalue. determined function parameters obtained select option order model (count)/year (count), or year (count)/model (count).

my end goal here generate html tree looks this:

  1. consigned (# of consigned)

    • 2007 (total count of 2007 models)
      • model 1 (count)
      • model 2 (count)
    • 2011 (total count of 2007 models)
  2. run (# of run)

    • 2007 (total count of 2007 models)
      • model 1 (count)
      • model 2 (count)
    • 2011 (total count of 2007 models)
  3. sold (same above)

the order of years should based on total count of models year (highest # first). similarly, order of models under years should ordered highest count.

alternatively, if user selects model/year grouping, i'd generate:

  1. consigned (# of consigned)

    • model1 (total count of model years)
      • year 1 (count of models year)
      • year 2 (count of models year)
    • model2 (total count of model years)
  2. run (# of run)

    • model1 (total count of model years)
      • year 1 (count of models year)
      • year 2 (count of models year)
    • model2 (total count of model years)
  3. sold (same above)

the ordering of based on counts well. let me know if need clarification! thanks!

the root of want simple. want group data property. makes seem complicated want @ several levels.

first have 3 set of data "consigned", "run", "sold". solve 1 , solve all.

first have cars grouped model. within model have cars grouped year. again core of though want group data property.

you can use function that:

var cars = [             { year: 2007, model: "ford f-150" },             { year: 2011, model: "toyota camry" },             { year: 2007, model: "ford f-150" },             { year: 2007, model: "ford f-150" },             { year: 2005, model: "dodge ram" }         ];  function groupby(propertyname, array) {     var groupedelements = {};      for(var = 0; < array.length; i++) {         var element = array[i];         var value = element[propertyname];          var group = groupedelements[value];         if(group == undefined) {             group = [element];             groupedelements[value] = group;         } else {             group.push(element);         }     }      return groupedelements; }  var result = groupby("year", cars) 

the output this:

{     "2005": [         {             "year": 2005,             "model": "dodge ram"         }     ],     "2007": [         {             "year": 2007,             "model": "ford f-150"         },         {             "year": 2007,             "model": "ford f-150"         },         {             "year": 2007,             "model": "ford f-150"         }     ],     "2011": [         {             "year": 2011,             "model": "toyota camry"         }     ] } 

you can play in jsfiddle: http://jsfiddle.net/luisperezphd/jct2k/

you can accomplish want doing grouping @ each level. first group year. go through each of generate groups , group year. count length of values array.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -