【问题标题】:JS array of objects, group values by timestampJS对象数组,按时间戳分组值
【发布时间】:2019-04-15 11:16:56
【问题描述】:

我有一系列对象,例如:

var arr = 
[{timestamp: "2019-04-15T11:00:00.000Z", value:true},
{timestamp: "2019-04-15T12:00:00.000Z", value: false},
{timestamp: "2019-04-15T13:00:00.000Z", value: true},
{timestamp: "2019-04-15T14:00:00.000Z", value: true},
{timestamp: "2019-04-15T15:00:00.000Z", value: true},
{timestamp: "2019-04-15T16:00:00.000Z", value: false},
{timestamp: "2019-04-15T17:00:00.000Z", value: true},
{timestamp: "2019-04-15T18:00:00.000Z", value: true}...] etc

24 小时。 我需要显示开始和结束真值的时间,从值 false 应该是单独的一组时间:

对于 exp.:时间:11、13-15、17-18。

可以像这个数组那样排序吗?

目前我得到这样的数组: 开启时间:11、13、14、15、16、17、18。

const onValues = arr.filter(item => item.output === true );
const onValuesTimes = onValues.map(a => (moment(a.timestamp).format('HH:mm')));

【问题讨论】:

  • 请添加想要的结果(作为数据)。
  • 我不确定这个,我需要在前端显示这个。
  • 目前还不清楚您是否希望获得一个值数组或带有减号的联合值,或者其他什么。
  • 如果它们被false分隔,我需要在组中获取真值,这应该是单独的组,然后显示该组中的第一个和最后一个值。就像我的采样时间一样:11、13 -15。

标签: javascript arrays sorting


【解决方案1】:

您可以获取值的时间戳并构建 atart 和 end 项目,连接这些项目并连接结果。

var array = [{ timestamp: "2019-04-15T11:00:00.000Z", value: true }, { timestamp: "2019-04-15T12:00:00.000Z", value: false }, { timestamp: "2019-04-15T13:00:00.000Z", value: true }, { timestamp: "2019-04-15T14:00:00.000Z", value: true }, { timestamp: "2019-04-15T15:00:00.000Z", value: true }],
    result = array
        .reduce((r, b, i, { [i - 1]: a }) => {
            if (!b.value) return r;
            if (a && a.value) {
                r[r.length - 1][1] = b.timestamp.slice(11, 16);
            } else {
                r.push([b.timestamp.slice(11, 16)]);
            }
            return r;
        }, [])
    .map(a => a.join('-'))
    .join(', ');

console.log(result);

【讨论】:

  • 多谢 mil Nina,这很好。只是我不能标记正确的答案。
【解决方案2】:

无耻插件:你可以使用我的ts-iterable-functions 库:groupAdjacent 函数就是为此而生的

const {pp, groupAdjacent, _map, map, _count, _single, _first, _last, filter} = 
   tsIterableFunctions
var arr = [
  {timestamp: "2019-04-15T11:00:00.000Z", value: true},
  {timestamp: "2019-04-15T12:00:00.000Z", value: false},
  {timestamp: "2019-04-15T13:00:00.000Z", value: true},
  {timestamp: "2019-04-15T14:00:00.000Z", value: true},
  {timestamp: "2019-04-15T15:00:00.000Z", value: true},
  {timestamp: "2019-04-15T16:00:00.000Z", value: false},
  {timestamp: "2019-04-15T17:00:00.000Z", value: true},
  {timestamp: "2019-04-15T18:00:00.000Z", value: true}
]

const q=pp(
  arr,
  groupAdjacent(
    x => x.value,
    x => x,
    (key,elements) => ({
      key,
      values : _map(elements, e => moment(e.timestamp).utc().hour())
    })
  ),
  filter(x => x.key), // == true
  map(
      x => `${ _first(x.values) }-${ _last(x.values) + 1 }`
  )
)
console.log([...q])
<script src="https://cdn.jsdelivr.net/npm/ts-iterable-functions@2"></script>
<script src="https://cdn.jsdelivr.net/npm/moment"></script>

【讨论】:

  • 再问一个问题。是否可以打印而不是 15:00,下一个值 16:00?那个打印是 11, 13-16, 17-19 ?
  • @KristinaAs 是的。那么,从逻辑上讲,这应该是 11-12、13-16、17-19 吗?查看我的编辑。
猜你喜欢
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多