【问题标题】:Summarizing the array + counting the instances总结数组+计算实例
【发布时间】:2019-01-18 03:14:28
【问题描述】:

我有一个包含电话和时间戳的数组,我已经四舍五入到最近的四分之一。

数组输入附加在我的 cmets 的末尾。

我正在尝试做的事情相当复杂,我尝试使用 map/filter/reduce 函数的组合,但不确定我应该以什么顺序来处理它。

我想要的是以下内容,按时间计算每部手机的数量,按当天的热门手机排序 - 此外,对于不存在的时间,它必须显示 0。

理想输出:

[{
  "OPPO R11s": [{
    "time": 00:00,
    "count": 20
  }, {
    "time": 00:15,
    "count": 13
  }, {
    "time": 00:30,
    "count": 0
  }, {
    "time": 00:45,
    "count": 23
  }], 
  "iPhone 7": [{
        "time": 00:00,
        "count": 20
      }]
}]

输入:

[
  {
    "phone": "OPPO R11s",
    "roundedTime": "10:45"
  },
  {
    "phone": "iPhone 7",
    "roundedTime": "03:15"
  },
  {
    "phone": "Samsung Galaxy XCover 4",
    "roundedTime": "11:45"
  },
  {
    "phone": "iPhone XS Max",
    "roundedTime": "12:00"
  },
  {
    "phone": "iPhone XS",
    "roundedTime": "01:00"
  },
  {
    "phone": "Samsung Galaxy Note9",
    "roundedTime": "02:30"
  },
  {
    "phone": "Samsung Galaxy J4",
    "roundedTime": "07:15"
  },
  {
    "phone": "iPhone XS Max",
    "roundedTime": "03:00"
  },
  {
    "phone": "Samsung Galaxy J4 Plus",
    "roundedTime": "02:45"
  },
  {
    "phone": "iPhone 6s",
    "roundedTime": "12:45"
  },
  {
    "phone": "iPhone XS Max AirPods Bundle",
    "roundedTime": "01:00"
  }]

进一步解释:

  • 我希望按照数组中出现的总次数对手机进行排名(即,如果 iPhone 出现次数最多,它将位于输出数组的顶部)。
  • 时间间隔为 15 分钟,因此它只会遍历数组并计算我们在 00:15 期间拥有的“iPhone 6s”数量。

【问题讨论】:

  • 我不清楚您所说的“当今顶级手机”是什么意思。你的指标是什么? count 和季度时间来自哪里?
  • 在 cmets @ggorlen 中更新
  • 对不起,我还是迷路了。 "OPPO R11s""IPhone 7" 在结果数组中,但它们在输入数组中各只出现 1 次,其中包含输入数组中不存在的数据数组。输入数组中的所有其他内容都被丢弃了——为什么?
  • 抱歉,输入数组只是一个子集,OPPO R11s有可能在10:45出现不止一次
  • 好吧,我假设这么多,但是为什么不发布一个完整的 I/O 以便我们可以按照您的逻辑进行操作呢?您还没有解释为什么大部分输入在输出中被丢弃。 “Oppo R11 不可能在 10:45 内出现不止一次”。请详细说明,我觉得这不是常识。我不清楚你在这里问什么。

标签: javascript arrays json sorting


【解决方案1】:

好的 - 我想你想要的是这个:

包含电话名称的对象数组以及使用该电话的所有时间的列表。数组应该排序。

首先通过输入,使用计数器将条目添加到地图中。每次该电话出现时,搜索其时间列表。如果时间存在,增加它,如果不增加时间。然后将电话列表映射到数组,其中包含将电话数据包装到电话名称的对象。移动计数和排序

const phoneMap = {};

const log = [
  {
"phone": "OPPO R11s",
"roundedTime": "10:45"
  },
  {
"phone": "iPhone 7",
"roundedTime": "03:15"
  },
  {
"phone": "Samsung Galaxy XCover 4",
"roundedTime": "11:45"
  },
  {
"phone": "iPhone XS Max",
"roundedTime": "12:00"
  },
  {
"phone": "iPhone XS",
"roundedTime": "01:00"
  },
  {
"phone": "Samsung Galaxy Note9",
"roundedTime": "02:30"
  },
  {
"phone": "Samsung Galaxy J4",
"roundedTime": "07:15"
  },
  {
"phone": "iPhone XS Max",
"roundedTime": "03:00"
  },
  {
"phone": "Samsung Galaxy J4 Plus",
"roundedTime": "02:45"
  },
  {
"phone": "iPhone 6s",
"roundedTime": "12:45"
  },
  {
"phone": "iPhone XS Max AirPods Bundle",
"roundedTime": "01:00"
  }];

log.forEach(function(logItem) {
  if (!phoneMap[logItem["phone"]]) {
    phoneMap[logItem["phone"]] = {count:1, times:[]};
  } else {
  	phoneMap[logItem["phone"]]["count"]++
  }

  var timeNode = phoneMap[logItem["phone"]]["times"].find(function(timeNode) {
    return timeNode["time"] === logItem["roundedTime"];
  })

  if (timeNode) {
    timeNode["count"]++;
  } else {
    phoneMap[logItem["phone"]]["times"].push({
      "time": logItem["roundedTime"],
      "count":1
    });
  }
})

var result = Object.keys(phoneMap).map(function(key){
	let obj = {};
  obj[key] = phoneMap[key];
  obj["count"] = phoneMap[key].count;
  delete phoneMap[key]["count"];
	return obj;
}).sort(function(a,b){
	return b.count - a.count;
});


console.log(result);

【讨论】:

    猜你喜欢
    • 2017-01-03
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    相关资源
    最近更新 更多