这在单行问题中很难说,但我正在寻找一些建议/最佳实践来构建数据和在Javascript中编写函数。
我有几个项目会定期更改状态。我的数据包含itemID,时间戳和状态。我目前正在将其构造为一个对象数组(对于每个项目),其历史优先级包含时间戳和状态。 (见下文)。
我正在寻找一个功能,可以让我使用最近的最新更新轻松获取给定时间内每个对象的状态。我不确定我的数据结构是否允许这样做,或者如果它允许,我不知道如何编写该函数。 (对于这个例子,我要将我的时间戳缩短为4位数字)
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'}
]
我希望能够拥有这样的功能:
getStatus(1260);
然后回来
{1: 'in-use', 2: 'open', 3: 'reset'}
每个ID,根据查询时间之前的最新历史记录,传入时的状态。
我完全不依赖于这个数据结构。我还尝试将历史记录包含一个包含时间和状态的对象数组,但这意味着每次都必须循环遍历整个数组。我最大的问题是我的头脑正在推动我采用SQL方法来实现这一目标,但我仍陷入客户端Javascript ......
我的问题:这是什么最好的数据结构?我将如何编写getStatus()函数?
谢谢!
答案 0 :(得分:2)
我还尝试将历史记录包含一个包含时间和状态的对象数组,但这意味着我每次都必须遍历整个数组。
如果您对数组进行了排序,则不会,因为您可以直接访问最近的日期。您还可以使用二进制搜索来获取特定时间戳的状态。使用您当前拥有的对象,您始终必须枚举所有属性以找到最佳匹配。
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;
}
答案 1 :(得分:0)
您可以使用循环:
// for each entry:
var n2 = n;
while (typeof items[n2] == "undefined" && n2 >= -1) {
n2--;
}
if (n != -1) {returnedArray[idBeingProcessed] = items[n2];}
else {alert("Error handling here");}
// repeat for each item (id 1, id 2...)
如果找到答案,这将停止。它可能效率低下,但嘿,它有效: - )
另外,如果适用,请考虑为history
对象使用数组。