views:

67

answers:

3

How do I parse through an array and for each element multiply its value field by 'x'

I want to pass through a a native array that contains 60 elements as follows:

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

In the above example the key field will be a number starting at 60 and counting down to 0.

The value field can be any number.

This array is built live starting from when it is passed it's first value, holding up to a maximum of 60 values elements, with the key for each value starting at 60.

Every seccond a new value is passed to the array and the oldest value is taken out. 0 is removed 60 is added and everything else is bumped down by 1.

Whenever this update takes place of adding a new value to the array, I also want to parse through the entire array and multiply the "value" field by 'x'. Lets say for example purposes that x is equal to 1.2.

This means that everytime the update is run the first value is multiplied by 1.2, the 2nd element's value is multiplied by 1.2 the third elements is multiplied by 1.2 and so on.

Meaning that by the time it reaches a "key" of 0 being the last element it will have been multiplied by 1.2 60 times.

Example of actual data:

[[-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]]

Current code:

This is my current usage for building the array:

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 //
   });
}
+1  A: 

I don't think you can do what you're asking. If read your code correctly, each second you're rebuilding the array from JSON data. It's trivial to loop through the array afterwards and multiply each element, but a second later your going to rebuild the array with the new data and lose those multiples.

You could simulate the effect by doing this:

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

Not sure if this is what you want, though.

Andrew Cooper
You are correct about rebuilding the array, and this works perfectly. Nice and simple also.
Thqr
A: 

Why did you double post?

var i,x;
x = 1.2;
for(i=60;i>=0;DnDArray[i--]['value']*=x) {/* no code required */}
Andrew Dunn
Removed old post as it was not "descriptive" enough.
Thqr
+1  A: 

you could start with this idea

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

then you could do something like,

​$.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.
    } 
});​

looks like this,

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

Reigel