Package Exports
- count-objects
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (count-objects) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
count-objects
Counts unique objects for each key and value of an array of objects
install
npm install count-objectsusage
constructor
const { CountObjects } = require("count-objects");
const objects = [
{
fruit: {
olive: "Arbequina",
apple: "Lady Alice",
orange: "Valencia",
},
},
{
fruit: {
olive: "Kalamata",
apple: "Lady Alice",
},
},
];
const co = new CountObjects(objects);count
// the count result in an object format:
const countObject = co.count();
console.log((countObject);
// {
// fruit: {
// olive: { Arbequina: 1, Kalamata: 1 },
// apple: { 'Lady Alice': 2 },
// orange: { Valencia: 1 }
// }
// }table
// the same data can be presented as a table:
const countTable = co.table();
console.table(countTable);
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │ key │ value │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │ 0 │ 'fruit.apple' │ 'Lady Alice' │ 2 │
// │ 1 │ 'fruit.olive' │ 'Arbequina' │ 1 │
// │ 2 │ 'fruit.olive' │ 'Kalamata' │ 1 │
// │ 3 │ 'fruit.orange' │ 'Valencia' │ 1 │
// └─────────┴────────────────┴──────────────┴───────┘add
// add more values to the counting object:
co.add([
{
fruit: {
orange: "Valencia",
apple: "Lady Alice",
},
},
]);
console.table(co.table());
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │ key │ value │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │ 0 │ 'fruit.apple' │ 'Lady Alice' │ 3 │
// │ 1 │ 'fruit.olive' │ 'Arbequina' │ 1 │
// │ 2 │ 'fruit.olive' │ 'Kalamata' │ 1 │
// │ 3 │ 'fruit.orange' │ 'Valencia' │ 2 │
// └─────────┴────────────────┴──────────────┴───────┘addFilter
// add a filter to count only objects with 'Valencia' oranges
// the filter format is an array, like in this example:
const valenciaFilter = ["fruit", "orange", "Valencia"];
co.addFilter(valenciaFilter);
console.table(co.table());
// count only objects that have
// key: 'fruit.orange'
// value: 'Valencia'
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │ key │ value │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │ 0 │ 'fruit.apple' │ 'Lady Alice' │ 2 │
// │ 1 │ 'fruit.olive' │ 'Arbequina' │ 1 │
// │ 2 │ 'fruit.olive' │ 'Kalamata' │ 0 │
// │ 3 │ 'fruit.orange' │ 'Valencia' │ 2 │
// └─────────┴────────────────┴──────────────┴───────┘
// add another filter, now for "Arbequina" olive:
co.addFilter(["fruit", "olive", "Arbequina"]);
console.table(co.table());
// ┌─────────┬────────────────┬──────────────┬───────┐
// │ (index) │ key │ value │ count │
// ├─────────┼────────────────┼──────────────┼───────┤
// │ 0 │ 'fruit.apple' │ 'Lady Alice' │ 1 │
// │ 1 │ 'fruit.olive' │ 'Arbequina' │ 1 │
// │ 2 │ 'fruit.olive' │ 'Kalamata' │ 0 │
// │ 3 │ 'fruit.orange' │ 'Valencia' │ 1 │
// └─────────┴────────────────┴──────────────┴───────┘getFilters
// see the current filters:
console.log(co.getFilters());
// [
// [ 'fruit', 'orange', 'Valencia' ],
// [ 'fruit', 'olive', 'Arbequina' ]
// ]