【问题标题】:Count duplicate values in an array and return count with an additional value using Javascript使用 Javascript 计算数组中的重复值并返回带有附加值的计数
【发布时间】:2020-03-30 07:13:25
【问题描述】:

我有以下array,我想返回一个新数组,其中包含重复的ids 的计数以及id 的值:

const things = [
  {
    id: 1,
    title: 'Something',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 2,
    title: 'Another thing',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 3,
    title: 'Yet another thing',
    categoryId: 2,
    categoryTitle: 'Category 2'
  },
  {
    id: 4,
    title: 'One more thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  },
  {
    id: 5,
    title: 'Last thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  }
]

我已经设法组合了一个简单的函数,它返回重复 ids 的计数(见下文),但它也返回 id(即1, 2, 4):

function categoriesCount (things) {
  const thingsMapped = things.map(thing => thing.categoryId)
  return thingsMapped.reduce((map, val) => {
    map[val] = (map[val] || 0) + 1
    return map
  }, {})
}

console.log('categoriesCount', categoriesCount(things))

返回:

"categoriesCount" Object {
  1: 2,
  2: 1,
  4: 2
}

而我希望它返回:

"categoriesCount" Object {
  'Category 1': 2,
  'Category 2': 1,
  'Category 3': 2
}

注意:类别标题的数值(例如类别 3)可能与它的 id 值不匹配(例如,关于类别 3 的 4)。

我错过了什么?

非常感谢。

【问题讨论】:

  • 最后 2 个对象的标题不应该是“Category 4”以获得该输出吗?
  • 键是类别标签还是后跟 categoryId 的通用“类别”标签?
  • map[val]的所有实例替换为map['Categories ' + val]???

标签: javascript arrays count duplicates


【解决方案1】:

你可以直接取categoryTitle,而不用映射数组。

function categoriesCount (things) {
    return things.reduce((hash, { categoryTitle }) => {
        hash[categoryTitle] = (hash[categoryTitle] || 0) + 1;
        return hash;
   }, {});
}

const things = [{ id: 1, title: 'Something', categoryId: 1, categoryTitle: 'Category 1' }, { id: 2, title: 'Another thing', categoryId: 1, categoryTitle: 'Category 1' }, { id: 3, title: 'Yet another thing', categoryId: 2, categoryTitle: 'Category 2' }, { id: 4, title: 'One more thing', categoryId: 4, categoryTitle: 'Category 3' }, { id: 5, title: 'Last thing', categoryId: 4, categoryTitle: 'Category 3' }]

console.log(categoriesCount(things));
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • categoryTitle 考虑到输出似乎有些错误。看起来(尽管没有多大意义)他需要将通用标签类别与 categoryId 连接起来。
【解决方案2】:

您可以在单个Array.reduce 镜头中执行此操作,使用destructuring 获取所需的类别标题并相应地构建密钥。

以下代码假定 categoryTitle 在记录中是一致的,并且您希望按 categoryTitle 进行分组。 如果您不想这样做,请指定。

const things = [
  {
    id: 1,
    title: 'Something',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 2,
    title: 'Another thing',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 3,
    title: 'Yet another thing',
    categoryId: 2,
    categoryTitle: 'Category 2'
  },
  {
    id: 4,
    title: 'One more thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  },
  {
    id: 5,
    title: 'Last thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  }
];

const res = things.reduce((acc, {categoryTitle}) => {
   return acc[categoryTitle] = (acc[categoryTitle] || 0) + 1, acc;
}, {});
console.log('categoriesCount', res);

【讨论】:

    【解决方案3】:

    假设您的类别标题是一致的

    function categoriesCount (things) {
    const thingsMapped = things.map(thing => thing.categoryId)
      return thingsMapped.reduce((map, val) => {
        map[val] = (map[val] || 0) + 1
        return map
      }, {})
    }
    

    【讨论】:

      【解决方案4】:

      我认为map函数是不需要的,缺少的是使用attrcategoryTitle作为key:

      const things = [  {    id: 1,    title: 'Something',    categoryId: 1,    categoryTitle: 'Category 1'  },  {    id: 2,    title: 'Another thing',    categoryId: 1,    categoryTitle: 'Category 1'  },  {    id: 3,    title: 'Yet another thing',    categoryId: 2,    categoryTitle: 'Category 2'  },  {    id: 4,    title: 'One more thing',    categoryId: 4,    categoryTitle: 'Category 3'  },  {    id: 5,    title: 'Last thing',    categoryId: 4,    categoryTitle: 'Category 3'  }];
      function categoriesCount (things) {
        return things.reduce((map, {categoryTitle}) => {
          map[categoryTitle] = (map[categoryTitle] || 0) + 1;
          return map
        }, {})
      }
      
      console.log('categoriesCount', categoriesCount(things))
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

      • 类别标题并不总是与categoryId 匹配,例如Category 3 有一个id4,所以你的建议会导致'Category 4': 2,而我在'Category 3': 2 之后,所以完整的类别标题,不是从id 派生的。
      • @NeilMerton 你原来的例子建议相反。
      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 2018-08-03
      相关资源
      最近更新 更多