Как рассчитать длину сгруппированного массива в угловых или JavaScript? - PullRequest
0 голосов
/ 25 апреля 2018
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'
    }
]

Я хочу получить длину каждой группы.как мы можем получить это?

например array.group ['secound category']. length

Ответы [ 4 ]

0 голосов
/ 25 апреля 2018

хоть и долго, это должно быть более чистое решение:

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);
0 голосов
/ 25 апреля 2018

Вы можете сделать это с простым JavaScript. Используйте Array#reduce для создания группы по категориям, а затем вы можете просто посчитать количество элементов операторов, таких как:

group['first category'].length

Рабочая демоверсия:

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);
0 голосов
/ 25 апреля 2018

Используйте .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);
0 голосов
/ 25 апреля 2018

Вы можете использовать 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...