Javascript数组,解析每个元素乘以'x'的每个元素

时间:2010-09-21 03:51:35

标签: javascript jquery arrays

如何解析数组并为每个元素将其值字段乘以'x'

我想传递一个包含60个元素的本机数组,如下所示:

[
     [value, key], [value, key], [value, key] 
]

在上面的示例中,关键字段将是一个从60开始并向下计数到0的数字。

值字段可以是任意数字。

此数组从传递第一个值开始实时构建,最多可容纳60个值元素,每个值的键从60开始。

每隔一秒将新值传递给数组,并取出最旧的值。 0被删除60被添加,其他所有内容被减少1。

每当此更新发生在向数组添加新值时,我还想解析整个数组并将“value”字段乘以“x”。 让我们说例如x等于1.2。

这意味着每次运行更新时,第一个值乘以1.2,第二个元素的值乘以1.2,第三个元素乘以1.2,依此类推。

意味着当它到达0的“关键点”时,它将是最后一个元素,它将乘以1.2 60倍。

实际数据示例:

[[-30.4901691201296, 60], [-30.1833776214304, 59], [-29.7627840450973, 58], [-29.3947583209356, 57], [-28.9645892754055, 56], [-28.6354656536817, 55], [-28.2921821871286, 54], [-27.9905577131509, 53], [-27.7947946913668, 52], [-27.6543340290543, 51], [-27.6519828946371, 50], [-27.6173533427694, 49], [-27.5554196324518, 48], [-27.4347081962877, 47], [-27.3527616238956, 46], [-27.1500146810747, 45], [-26.9074687550566, 44], [-26.5520557024907, 43], [-26.3778269233317, 42], [-26.2025741177589, 41], [-25.9715337718657, 40], [-25.7909728444171, 39], [-25.6446160165696, 38], [-25.7040560541356, 37], [-25.8676731838619, 36], [-26.1857049460322, 35], [-26.5338463742982, 34], [-26.8991378853451, 33], [-27.0722352574209, 32], [-26.9933000067798, 31], [-26.5736545189266, 30], [-25.5369071865736, 29], [-24.0243166908922, 28], [-22.2063720253207, 27], [-20.4275559328569, 26], [-19.0900734751772, 25], [-17.9226541769101, 24], [-17.2615147887497, 23], [-16.8724851836878, 22], [-16.7577128443888, 21], [-17.1571347530026, 20], [-17.6471873975822, 19], [-18.6461197175468, 18], [-19.8885692353328, 17], [-21.2039894571651, 16], [-23.0079052401369, 15], [-25.1005655769037, 14], [-27.342591130044, 13], [-29.7388646710222, 12], [-32.1429579450835, 11], [-34.5906266190624, 10], [-37.0391503781189, 9], [-39.5315634976835, 8], [-41.9487262455882, 7], [-43.9289096382579, 6], [-46.00519229484, 5], [-48.6400387646249, 4], [-50.5736255352748, 3], [-52.8788720227602, 2], [-54.786535213712, 1]]

当前代码:

这是我目前用于构建数组的用法:

setInterval(ExampleUsage, 1000);

function ExampleUsage() {
    $.getJSON(urlDefault, function (data) {
          RawDnD = data.DnD* 1;
          DnDData.unshift(RawDnD);
          DnDData = DnDData.slice(0, 60);
          DnDArray = $.map(DnDData, function (n, i) {
              return [[n, 60 - i]];
          });
          // Parse array here //
   });
}

2 个答案:

答案 0 :(得分:1)

我认为你不能做你所要求的。如果正确读取代码,则每秒都要从JSON数据重建数组。之后循环遍历数组并乘以每个元素是微不足道的,但是一秒钟之后你将使用新数据重建数组并丢失这些数据。

您可以通过以下方式模拟效果:

var factor = 1.2;
var multiplier = factor;
for (i = 0; i < dataArray.length; i++) {
    dataArray[i] *= multiplier;
    multiplier *= factor;
}

不确定这是否是你想要的。

答案 1 :(得分:1)

你可以从这个想法开始

​$.each( arrayVariable ,function(i,v){
    console.log(v[0] + ' ---- ' + v[1] + '\n');
    // v[0] is the value
    // v[1] is the key 
});​

然后你可以做类似的事情,

​$.each( arrayVariable ,function(i,v){
    v[1] -= 1; // decrease all keys by 1
    v[0] *= factor; // factor = 1.2;
    if (v[1] == -1) { // if key was 0 before, then 0-1 is -1
        v[1] = 60; // replace it with 60, the new one...
        v[0] = newData * factor; // new data inserted, multiplied by the factor.
    } 
});​

看起来像这样,

setInterval(ExampleUsage, 1000);

function ExampleUsage() {
    $.getJSON(urlDefault, function (data) {
        RawDnD = data.DnD * 1;​
        // DnDData, is assumed to to have all values already
        $.each(DnDData, function (i, v) {
            v[1] -= 1; // decrease all keys by 1
            v[0] *= factor; // factor = 1.2;
            if (v[1] == -1) { // if key was 0 before, then 0-1 is -1
                v[1] = 60; // replace it with 60, the new one...
                v[0] = RawDnD * factor; // new data inserted, multiplied by the factor.
            }
        });​
    });
}

simple demo