处理连续的布尔值数组

时间:2018-12-20 16:10:40

标签: javascript algorithm

很难为此标题设置标题和描述,但是希望您在阅读代码和注释时会得到它。如果你们有更好的主意,请编辑。

对于该特定示例,我如何在javascript中获得每一天truefalse之间的时间戳总和差?

作为数组的一般规则:是否总是循环true-false。

我的真实数据要复杂一些,但是即使对于这个简化的示例,我似乎也无法正确思考。

可以减少,可以是for循环,任何东西。谢谢!

const data = [
    { day: 'today', timestamp: 11, value: true },
    { day: 'today', timestamp: 13, value: false }, //here should be 13-11
    { day: 'today', timestamp: 14, value: true },
    { day: 'today', timestamp: 17, value: false }, //here should be 17-14
    //the sum for today should be 5 (13-11 + 17-14)
    { day: 'tomorrow', timestamp: 9, value: true },
    { day: 'tomorrow', timestamp: 11, value: false }, //here should be 11-9
    { day: 'tomorrow', timestamp: 11, value: true },
    { day: 'tomorrow', timestamp: 16, value: false } //here should be 16-11 
    //the sum for today should be 7 (11-9 + 16-11)
]

3 个答案:

答案 0 :(得分:3)

一个简单的减少就可以完成工作:只需添加较大的'false'值,然后从总和中删除'true'值即可;)

const data = [
    { day: 'today', timestamp: 11, value: true },
    { day: 'today', timestamp: 13, value: false }, //here should be 13-11
    { day: 'today', timestamp: 14, value: true },
    { day: 'today', timestamp: 17, value: false }, //here should be 17-14
    //the sum for today should be 5 (13-11 + 17-14)
    { day: 'tomorrow', timestamp: 9, value: true },
    { day: 'tomorrow', timestamp: 11, value: false }, //here should be 11-9
    { day: 'tomorrow', timestamp: 11, value: true },
    { day: 'tomorrow', timestamp: 16, value: false } //here should be 16-11 
    //the sum for today should be 7 (11-9 + 16-11)
];

const result = data.reduce((acc, elt) => {
  if(!acc[elt.day]) acc[elt.day] = 0;
  if(!elt.value) {
    acc[elt.day] += elt.timestamp;
  } else {
    acc[elt.day] -= elt.timestamp;
  }
  return acc;
},{});
console.log(result);

不要忘记每天在累加器中将其初始化为0! 希望这会有所帮助。

答案 1 :(得分:1)

您可以轻松地reduce完成

const data = [
    { day: 'today', timestamp: 11, value: true },
    { day: 'today', timestamp: 13, value: false },
    { day: 'today', timestamp: 14, value: true },
    { day: 'today', timestamp: 17, value: false },
    { day: 'tomorrow', timestamp: 9, value: true },
    { day: 'tomorrow', timestamp: 11, value: false }, 
    { day: 'tomorrow', timestamp: 11, value: true },
    { day: 'tomorrow', timestamp: 16, value: false } 
]

const op = data.reduce((o,c)=>{
  if(o[c['day']]){
    o[c['day']]['timestamp'] += c.value ? -c.timestamp : c.timestamp;
  }  else {
    o[c['day']] = {
      'timestamp' : c.value ? -c.timestamp : c.timestamp
    }
  }
  return o;
},{})

console.log(op)

答案 2 :(得分:1)

您可以检查结果集中最后插入的对象并更新时间戳。

const
    data = [{ day: 'today', timestamp: 11, value: true }, { day: 'today', timestamp: 13, value: false }, { day: 'today', timestamp: 14, value: true }, { day: 'today', timestamp: 17, value: false }, { day: 'tomorrow', timestamp: 9, value: true }, { day: 'tomorrow', timestamp: 11, value: false }, { day: 'tomorrow', timestamp: 11, value: true }, { day: 'tomorrow', timestamp: 16, value: false }],
    result = data.reduce((r, { day, timestamp, value }) => {
        var last = r[r.length - 1];
        if (!last || last.day !== day) {
            r.push(last = { day, timestamp: 0 });
        }
        last.timestamp += value ? -timestamp : +timestamp;
        return r;
    }, []);

console.log(result);