Manish Salunke
Manish Salunke

Reputation: 63

how to count length of grouped array in angular or javascript?

array = [  
  {
    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'
    }
]

i want get length of each group. how we can get that?

for example array.group['secound category'].length

Upvotes: 2

Views: 937

Answers (4)

Ram
Ram

Reputation: 132

though lengthy this should be cleaner solution:

var array = [  
  {
    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'
    }
];

var name = checked = color = group = 0;
array.forEach(function(item){
if(item.name){
    name++;
}
if(item.checked){
    checked++;
}
if(item.color){
    color++;
}
if(item.group){
    group++;
}
});
console.log(name);
console.log(checked);
console.log(color);
console.log(group);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370819

Use .filter if you need to find the length of just one particular group name, or reduce if you need to figure out all of them:

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);

Upvotes: 1

31piy
31piy

Reputation: 23859

You can do it with plain JavaScript. Use Array#reduce to create a group by categories and then you can simply count the number of elements statements such as:

group['first category'].length

Working demo:

const array = [  
  {
    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'
    }
];

const group = array.reduce((acc, item) => {
  if (!acc[item.group]) {
    acc[item.group] = [];
  }
  
  acc[item.group].push(item);
  return acc;
}, {});

console.log(group['first category'].length);
console.log(group['second category'].length);

Upvotes: 0

Faly
Faly

Reputation: 13356

You can use array.filter:

var array = [  
  {
    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'
    }
];

var len1 = array.filter(e => e.group === 'first category').length;
console.log(len1);
var len2 = array.filter(e => e.group === 'second category').length;
console.log(len2);

Upvotes: 2

Related Questions