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' ]
// ]clearFilters
// removes all filters
co.clearFilters();
console.log(co.getFilters());
// []removeFilter
// removes a specific filter if it exists:
co.addFilter(["a", 1]);
co.addFilter(["b", 2]);
co.addFilter(["c", 3]);
console.log(co.getFilters());
// [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
co.removeFilter(["b", 2]);
console.log(co.getFilters());
// [ [ 'a', 1 ], [ 'c', 3 ] ]count unique values
// instead of counting unique objects, we can count unique values.
// in this example we count unique colors:
const flowers = [
{
color: "black",
type: "Tulip",
},
{
color: "black",
type: "Tulip",
},
{
color: "white",
type: "Tulip",
},
{
type: "Tulip",
},
];
const uniqueColors = new CountObjects(flowers, { uniqueKey: "color" });
console.table(uniqueColors.table());
// ┌─────────┬────────┬─────────┬───────┐
// │ (index) │ key │ value │ count │
// ├─────────┼────────┼─────────┼───────┤
// │ 0 │ 'type' │ 'Tulip' │ 2 │
// └─────────┴────────┴─────────┴───────┘
// about the unique key ('uniqueKey'):
// 1. it needs to be at the base of the object (not nested)
// 2. it is omitted from the count
// 3. if it is missing, the values in the object are not counted
// 4. it can only be set once, at the constructorclone
// creates a clone of the countObjects instance
// this can be helpful when setting a state with React
const clone = co.clone();
console.log(clone === co);
// false