【问题标题】:Count duplicates in an array and return new array with a key value derived from another array in Javascript计算数组中的重复项并返回具有从 Javascript 中的另一个数组派生的键值的新数组
【发布时间】:2020-03-30 13:16:27
【问题描述】:

继我之前的问题之后,我想更改和扩展建议的功能。

这是我得到的数据:

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

const categories = [
  {
    id: 1,
    title: 'Category 1'
  },
  {
    id: 2,
    title: 'Category 2'
  },
  {
    id: 4,
    title: 'Category 3'
  }
]

之前我已经被展示了如何按照这些思路做某事:

const duplicatesCountWithTitle = (things, categories) => {
  const thingsReduced = things.reduce((hash, { categoryId }) => {
    hash[categoryId] = (hash[categoryId] || 0) + 1
    return hash
  }, {})
}

如您所知,问题在于它实际上返回了一个新对象,而不是一个新数组。此外,我想将categories 数组中的categoryTitlethings 数组中的重复计数结果结合起来,基于categoryId 匹配categories 中的id

  // currently the above returns an object in the structure of:
  // {
  //   1: 2,
  //   2: 1,
  //   4: 2
  // }
  // what I'm after is an array like this:
  // [
  //   { 'Category 1': 2 },
  //   { 'Category 2': 1 },
  //   { 'Category 3': 2 }
  // ]

再次感谢您。

【问题讨论】:

  • 所需的结果是否只是每个类别的计数器?
  • @Trace - 期望的结果是:[ { 'Category 1': 2 }, { 'Category 2': 1 }, { 'Category 3': 2 } ] 其中值是匹配每个类别的事物的计数。

标签: javascript arrays duplicates derived


【解决方案1】:

这样的?

const newArr = categories.map(category => {
    const count = things.filter(thing => thing.categoryId === category.id).length;
    return { [category.title]: count }
});
console.log(newArr);

https://jsfiddle.net/f3x6m12j/

【讨论】:

  • 感谢@Trace - 我对您的代码进行了一些调整以使其更易于使用:const newArr = categories.map(category => { const count = things.filter(thing => thing.categoryId === category.id).length; return { title: category.title, value: count } });,因为我在 Vue.js 的 v-for 循环中使用了 array .
  • 没问题,随便用吧。
【解决方案2】:

您可以将Map 用于idtitle 的关系。

const
    duplicatesCountWithTitle = (things, categories) => things.reduce((hash, { categoryId }) => {
        hash[categories.get(categoryId)] = (hash[categories.get(categoryId)] || 0) + 1
        return hash;
    }, {}),
    things = [{ id: 1, title: 'Something', categoryId: 1 }, { id: 2, title: 'Another thing', categoryId: 1 }, { id: 3, title: 'Yet another thing', categoryId: 2 }, { id: 4, title: 'One more thing', categoryId: 4 }, { id: 5, title: 'Last thing', categoryId: 4 }],
    categories = [{ id: 1, title: 'Category 1' }, { id: 2, title: 'Category 2' }, { id: 4, title: 'Category 3' }],
    result = duplicatesCountWithTitle(
        things,
        categories.reduce((m, { id, title }) => m.set(id, title), new Map)
    );

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

【讨论】:

    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2020-03-30
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多