2
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

3
  • Should it output 2, 4 or 8? Commented Apr 25, 2018 at 7:08
  • there are 2 group group:'first category' and group:'second category'. want to count these length Commented Apr 25, 2018 at 7:10
  • 1
    Use underscore library Commented Apr 25, 2018 at 7:10

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you.. i want one more solution for.. if i have length total 4 then i want to display only 2 count as text then we can do that?
1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.