首页 > 解决方案 > 在对象数组中创建唯一列表和数组项计数

问题描述

我有以下源数组:

const list = [
    {
        students: [ 'peter', 'bob', 'john']
    },
    {
        students: [ 'thomas', 'sarah', 'john']
    },
    {
        students: [ 'john', 'sarah', 'jack']
    }
];

我想得到唯一的学生姓名和他们的人数,最终结果应该是这样的:

{
    'john': 3,
    'sarah': 2,
    'thomas': 1,
    'jack': 1,
    'peter': 1,
    'bob': 1
}

这是我的尝试:

const unique = list.reduce(function(total, curr){
  const students = curr.students;
  for (c of students) {
    if (!total[c]) {
      total[c] = 1
    } else {
      total[c] += 1;
    }
  }
  return total;

}, {});

有更好的方法吗?还是更快更清洁的方式?谢谢

标签: javascriptarraysfor-loopecmascript-6reduce

解决方案


我会先展平阵列,然后计算reduce

const list = [
    {
        students: [ 'peter', 'bob', 'john']
    },
    {
        students: [ 'thomas', 'sarah', 'john']
    },
    {
        students: [ 'john', 'sarah', 'jack']
    }
];


const allStudents = list.flatMap(({ students }) => students);
const count = allStudents.reduce((a, name) => {
  a[name] = (a[name] || 0) + 1;
  return a;
}, {});
console.log(count);

如果您还希望对属性进行排序,则获取Object.entries对象的 ,对其进行排序,然后将其转回具有 的对象Object.fromEntries

const list = [
    {
        students: [ 'peter', 'bob', 'john']
    },
    {
        students: [ 'thomas', 'sarah', 'john']
    },
    {
        students: [ 'john', 'sarah', 'jack']
    }
];


const allStudents = list.flatMap(({ students }) => students);
const count = allStudents.reduce((a, name) => {
  a[name] = (a[name] || 0) + 1;
  return a;
}, {});
const sorted = Object.fromEntries(
  Object.entries(count).sort((a, b) => b[1] - a[1])
);
console.log(sorted);

如果您的环境不支持 flatMap 或 fromEntries,请使用 polyfill 或 flatten/group 使用不同的方法:

const list = [
    {
        students: [ 'peter', 'bob', 'john']
    },
    {
        students: [ 'thomas', 'sarah', 'john']
    },
    {
        students: [ 'john', 'sarah', 'jack']
    }
];


const allStudents = [].concat(...list.map(({ students }) => students));
const count = allStudents.reduce((a, name) => {
  a[name] = (a[name] || 0) + 1;
  return a;
}, {});
const sortedEntries = Object.entries(count).sort((a, b) => b[1] - a[1]);
const sortedObj = sortedEntries.reduce((a, [prop, val]) => {
  a[prop] = val;
  return a;
}, {});
console.log(sortedObj);

请记住,对象属性顺序仅在 ES6+ 环境中指定。虽然Object.fromEntries规范不保证以与条目相同的顺序创建对象,但幸运的是,在我遇到的任何实现中,它无论如何都可以。(如果您仍然担心它,您可以使用老式reduce方法来创建对象,就像在第三个片段中一样)


推荐阅读