代码之家  ›  专栏  ›  技术社区  ›  Sphvn Frebin Francis

Javascript数组,解析每个元素将每个元素乘以“x”

  •  0
  • Sphvn Frebin Francis  · 技术社区  · 14 年前

    如何通过数组进行解析,并将每个元素的值字段乘以“x”

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

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

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

    值字段可以是任意数字。

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

    每当这个更新代替向数组添加新值时,我还想解析整个数组,并用“value”字段乘以“x”。

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

    也就是说,当它到达最后一个元素0的“键”时,它将被乘以1.260倍。

    [[-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 回复  |  直到 14 年前
        1
  •  1
  •   Andrew Cooper    14 年前

    我认为你不能按你的要求做。如果正确地读取了代码,那么每一秒都是从JSON数据重建数组。之后循环遍历数组并将每个元素相乘是很简单的,但是一秒钟之后,您将使用新数据重建数组并丢失这些倍数。

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

    不过,我不确定这是不是你想要的。

        2
  •  1
  •   Reigel Gallarde    14 年前

    你可以从这个想法开始

    ​$.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