JavaScript 自动更新数组
我想做以下事情;
- 创建一个任意有限长度(例如 10 行)的 2 列数组,
- 填充后从恒定速率数据流中顺序填充它
- ,从同一数据流更新它(即替换元素 0,向下移动 1-9,丢弃旧的 9 )
- (最佳)输出每列的平均值
我自己可能可以做 4,但不知道如何做 1-3。
如果有帮助,我正在尝试翻译它;
http://kindohm.com/2011/03/01/KinectCursorControl.html (参见可怕的晃动光标下方)。
I'd like to do the following;
- create a 2 -column array of arbitrary finite length (say, 10 rows)
- populate it sequentially from a constant-rate datastream
- once its populated, update it from the same datastream ( ie replace element 0, move 1-9 down, discard old 9)
- (optimally) output an average for each column
I can probably do 4 myself, but have no idea how to do 1-3.
If it helps, I'm trying to translate this;
http://kindohm.com/2011/03/01/KinectCursorControl.html (see under the dreaded shaking cursor).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以正常工作(顺便说一句,这是个好问题 - 有趣的小挑战,因为有很多方法可以做到这一点)
它基本上所做的是创建一个对象并将其分配给pointSmoother。该对象有 4 个方法:
pushPoint()
、clear()
、getPoints()
和average()
。在顶部,您可以设置一个点有多少个坐标,以及要保留多少个点(最大)。我用了你的例子,每个点 2 个坐标,一次 10 个点。现在,我假设您一次获取 2 个一组的值。我们将这两个值称为
x
和y
。当您收到它们时,使用pointSmoother.pushPoint(x, y);
将它们添加到事物中。然后,您可以通过调用 pointSmoother.average() 来获取“平均点”,它将返回一个包含(在本例中)2 个值的数组:平均值 x 和平均值 y。如果您想自己查看数组,可以调用 pointSmoother.getPoints() ,它将返回点数组。最后,pointSmoother.clear() 将清空先前值的数组。
这是一个演示: http://jsfiddle.net/tapqs/1/
This should work ok (nice question, by the way - fun little challenge, since there are tons of ways to do it)
What it basically does is it creates an object and assigns it to
pointSmoother
. The object has 4 methods:pushPoint()
,clear()
,getPoints()
andaverage()
. At the top of the thing you can set how many coordinates a point has, and how many points (maximum) to keep. I used your example of 2 coordinates per point, and 10 points at a time.Now, I've made the assumption that you get your values in sets of 2 at a time. Let's call those 2 values
x
andy
. When you receive them, add them to the thing by usingpointSmoother.pushPoint(x, y);
. You can then get the "average point", by callingpointSmoother.average()
which will return an array with (in this case) 2 values: average x and average y.If you want to look at the array yourself, you can call
pointSmoother.getPoints()
which will return the points array. And lastly,pointSmoother.clear()
will empty the array of previous values.Here's a demo, of sorts: http://jsfiddle.net/tapqs/1/
那是一个二维数组。加载数据就像加载任何其他数组一样。
要按顺序更新它,您需要查看 javascript 中的队列行为。使用 unshift() 和 pop()。
Javascript 数组: http://www.w3schools.com/jsref/jsref_obj_array.asp
最后假设有 10 个位置,则对列进行平均:
That's a two dimensional array. Loading data works just like with any other array.
To update it sequentially you are looking at queue behavior in javascript. Use unshift() and pop().
Javascript Arrays: http://www.w3schools.com/jsref/jsref_obj_array.asp
Finally for averaging the columns assuming there are 10 positions: