【问题标题】:Compare the element of an Array of arrays and count the frequency of a element [duplicate]比较数组数组的元素并计算元素的频率[重复]
【发布时间】:2020-07-10 11:47:12
【问题描述】:

我的问题:

var rawData = [[two],[three],[four],[one],[two],[two],[two],[three],[four],[five],[three],[four],[five]];

假设我有这个 arrays of array,我必须从中找到数组的唯一元素以及数组中该唯一元素的总数,以两个各自数组的形式,例如 p>

var queslist = [one,two,three,four,five...]
var count = [countOfOne, countOfTwo, countOfThree, countOfFour, countOfFive...]

(请记住 rawData 是动态的,因为我从大型电子表格中获取数据) 实现这一目标的最佳方法应该是什么?

【问题讨论】:

  • 不要更改球门柱。

标签: javascript arrays google-apps-script google-sheets


【解决方案1】:

也许是这样的?这两个例子也可以用 reduce 来完成,我觉得这更难阅读。

let list = {}
const rawData = ["two","three","four","one","two","two","two","three","four","five","three","four","five"]
  .forEach(item => {
    const obj = list[item] || { count: 0 };
    obj.count = obj.count ? obj.count + 1 : 1;
    list[item] = obj;
  });


 ["one","two","three","four","five"].forEach(item => console.log(item,list[item]))

忽略比较数组中没有的东西

let list = {}
const compareList  = ["two","three"];
const rawData = ["two","three","four","one","two","two","two","three","four","five","three","four","five"]
  .forEach(item => {
    if (compareList.indexOf(item) !=-1) {
      const obj = list[item] || { count: 0 };
      obj.count = obj.count ? obj.count + 1 : 1;
      list[item] = obj;
    }
  });


 console.log(list)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 2014-03-01
    • 2019-09-12
    • 2015-02-05
    • 1970-01-01
    • 2016-03-25
    • 2013-11-27
    相关资源
    最近更新 更多