Skip to content Skip to sidebar Skip to footer

I Have An Array Of 100 Objects With Properties Of Category And Subcategory And Need To Create A New Array Of The Count Of Subcategories?

I have an array of 100 objects with category and subcategory properties and I want to create a new array with the name of each category and the number(count) of subcategories for t

Solution 1:

Maybe this works for you. It utilizes Array.prototype.reduce()

var data = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 }],
    count = data.reduce(function (r, a) {
        r[a.Category] = r[a.Category] || {};
        r[a.Category][a.SubCategory] = (r[a.Category][a.SubCategory] || 0) + 1;
        return r;
    }, {});

document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');

Just only the counts in Category:

var data = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 }],
    count = data.reduce(function (r, a) {
        r[a.Category] = (r[a.Category] || 0) + 1;
        return r;
    }, {});

document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');

Solution 2:

First count number of subcategories per category with reduce:

var subcategoriesCounts = subcats.reduce(function(acc, cur) {
    acc[cur.CategoryID] = (acc[cur.CategoryID] || 0) + 1;
    return acc;
}, {});

Then use map to transform categories to desired objects:

var result = categories.map(function(cat) {
    return {
        name: cat.Name,
        count: subcategoriesCounts[cat.ID] || 0
    }
});

Solution 3:

var arr = [{ ticketId: 1, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 2, Category: "Driver", CategoryID: 29, SubCategory: "Monitor", SubCategoryID: 31 }, { ticketId: 3, Category: "Hardware", CategoryID: 11, SubCategory: "Monitor", SubCategoryID: 32 }, { ticketId: 4, Category: "Hardware", CategoryID: 11, SubCategory: "phone", SubCategoryID: 13 } ];

var map = {}


for(var i = 0; i < arr.length; i++) {
  console.log(arr[i])
    if (!map[arr[i].Category]) {
      map[arr[i].Category] = {
        count: 1,
        parentId: arr[i].CategoryID,
        categoryId: arr[i].SubCategoryID,
        categoryName: arr[i].SubCategory
      };
    } else {
      map[arr[i].Category].count++;
    }
}

var results = [];
for (var category in map) {
  results.push(map[category]);
}

var el = document.getElementById("result").innerHTML = JSON.stringify(results, null, '  ')
<preid="result"></pre>

Solution 4:

//FORMAT

var list = [
    {
        "ticketID": 1, 
        "category": "driver", 
        "subcategories": ['toner','power','paper jam','office applications']
    },
    {
        "ticketID": 2, 
        "category": "driver", 
        "subcategories": ['toner','power']
    },
];

Then you can simply get the length of the subs by console.log(list[0].subcategories.length)

as well as access them console.log(list[0].subcategories[0])

Solution 5:

To combine the counts for both levels in the same structure you have to separate the counts from the children. It'll cause no end of confusion if you both store the top level count as a property, and also the children themselves.

A better structure is produced by this:

var counts = data.reduce(function(r, o) {
    var cat = o.Category;
    var sub = o.SubCategory;

    r[cat] = r[cat] || { count: 0, children: {} }
    r[cat].count++;

    r[cat].children[sub] = r[cat].children[sub] || { count: 0 }
    r[cat].children[sub].count++;
    return r;
}, {});

which for your data will produce:

{
  "Driver": {
    count:2,
    children: {
        "Monitor": { count:2 }
    },
  },
  "Hardware": {
    count:2,
    children: {
      "Monitor": { count:1},
      "phone": { count:1 },
    }
  }
}

and which supports additional levels of nesting if necessary.

Post a Comment for "I Have An Array Of 100 Objects With Properties Of Category And Subcategory And Need To Create A New Array Of The Count Of Subcategories?"