本文介绍了如何构建时间特定的数据,以便最新的一点可以找到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很难在单行问题的短语,但我正在寻找一些建议/最佳实践来构建数据和在Javascript中写一个函数。

This is kinda hard to phrase in a single line questions, but I'm looking for some advice/best practices for structuring data and writing a function in Javascript.

我有几个项目可以定期更改状态。我的数据包含一个itemID,timestamp和status。我目前正在将其构造为一组对象(每个项目),其历史优先级包含时间戳和状态。 (见下文)。

I have several items that change status regularly. My data contains an itemID, timestamp, and status. I am currently structuring it as an array of objects (for each item), with a history priority that contains the timestamps and status. (see below).

我正在寻找一个功能,让我可以在给定的时间使用最近的过去更新轻松获取每个对象的状态。我不知道我的数据结构是否允许这个,或者如果它是,如何写功能。 (为了这个例子,我将缩短我的时间戳为4位数字)

I'm looking for a function that will allow me to easily get the status of each object at a given time using the most recent past update. I'm not sure if my data structure will allow for this, or if it does, how to write the function. (for this example I'm going to shorten my timestamps to a 4 digit number)

 var items = [
      { id: 1,
        history: {1234: 'open', 1256: 'in-use', 1289: 'reset', 1293: 'open'},
      { id: 2,
        history: {1230: 'open', 1290: 'in-use'},
      { id: 3,
        history: {1238: 'open', 1241: 'in-use', 1251: 'reset'}
 ]

我想要有一个函数如下:

I'd like to be able to have a function like this:

 getStatus(1260);

并返回

 {1: 'in-use', 2: 'open', 3: 'reset'}

根据查询时间之前的最新历史记录,每个ID都具有当前所处的状态。

Each id, with the status it was in at the time passed in based on the most recent history record prior to the queried time.

我是完全没有附加到这个数据结构。我也尝试历史上包含时间和状态的对象数组,但这意味着我每次都必须遍历整个数组。我最大的问题是我的头正在推动我使用SQL方法,但是我被困在客户端的Javascript ...

I am not at all attached to this data structure. I also tried having the history an array of objects containing the time and the status, but that means I have to loop through the entire array every time. My biggest problem is that my head is pushing me to an SQL method for doing this, but I'm stuck in client-side Javascript...

我的问题:什么是最好的数据结构吗?我如何去写我的getStatus()函数?

My questions: What is the best data structure for this? and How would I go about writing my getStatus() function?

谢谢!

推荐答案

如果您将数组排序,则不能直接访问最近的日期。此外,您可以使用二进制搜索获取特定时间戳的状态。使用您目前拥有的对象,您始终必须枚举所有属性以找到最佳匹配。

Not if you had the array sorted, as you then can access the most recent date directly. Also you can use binary search for getting the state at a specific timestamp. With the object you currently have, you always have to enumerate all properties to find the best-matching.

var items = [
  { id: 1,
    history: [
      { timestamp: 1234, status: 'open'},
      { timestamp: 1256, status: 'in-use'},
      { timestamp: 1289, status: 'reset'},
      { timestamp: 1293, status: 'open'}
    ]
  },
  …
];
function index(arr, compare) { // binary search, with custom compare function
    var l = 0,
        r = arr.length - 1;
    while (l <= r) {
        var m = l + ((r - l) >> 1);
        var comp = compare(arr[m]);
        if (comp < 0) // arr[m] comes before the element
            l = m + 1;
        else if (comp > 0) // arr[m] comes after the element
            r = m - 1;
        else // this[m] equals the element
            return m;
    }
    return l-1; // return the index of the next left item
                // usually you would just return -1 in case nothing is found
}
// example:
function insertItem(history, timestamp, status) {
    var i = index(history, function(item) {
        return item.timestamp - timestamp;
    });
    history.splice(i+1, 0, {timestamp: timestamp, status: status});
}

function getStatus(time) {
    var result = {};
    function comparefn (item) {
        return item.timestamp - time;
    }
    for (var i=0; i<items.length; i++) {
        var pos = index(items[i].history, comparefn);
        result[items[i].id] = pos == -1
          ? undefined
          : items[i].history[pos].status;
    }
    return result;
}

这篇关于如何构建时间特定的数据,以便最新的一点可以找到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:24