makeTallyByProperty

Creates a tally (count) of objects in an array

Given an array of objects, this function makes a tally or counts the occurrence of similar objects based on a given property.

import { makeTallyByProperty } from "1loc";

const arr = [
    { brand: 'audi', model: 'q8', year: '2019' },
    { brand: 'audi', model: 'rs7', year: '2020' },
    { brand: 'ford', model: 'mustang', year: '2019' },
    { brand: 'ford', model: 'explorer', year: '2020' },
    { brand: 'bmw', model: 'x7', year: '2020' },
]

makeTallyByProperty(arr, 'brand') 

// { 'audi': 2, 'ford': 2, 'bmw': 1 }

Last updated