Используйте .filter
, если вам нужно найти длину только одного конкретного имени группы, или reduce
, если вам нужно выяснить все из них:
const input = [
{
name: 'hello 1',
checked: true,
color:'#1ac3ec',
group:'first category'
},
{
name: 'hello 2',
checked: true,
color:'#7dc55c',
group:'first category'
},
{
name: 'hello 3',
checked: true,
color:'#005073',
group:'second category'
},{
name: 'hello 4',
checked: true,
color:'#fbb330',
group:'second category'
},{
name: 'hello 4',
checked: true,
color:'#fbb330',
group:'sole category'
}
];
const groups = input.reduce((accum, { group }) => {
accum[group] = (accum[group] || 0) + 1;
return accum;
}, {});
console.log(groups);