Dataset Transformation Map

Overview


Map
The first example calculates the dataset aggregate wihtin the map function. The function passed to map takes three arguments, the current item, the current index, and the full dataset.

In this example, a 3 period moving average is calculated by taking the last 3 elements of the data argument for each item and calculating an average.

This method of calculating is slower than other methods, becuase it recalculates the average from scratch for each item in the list, without re-using any part of the calculating between items.



let data = [{price:100},{price:101},{price:100},{price:102},{price:102},{price:103},{price:105},{price:102}];

let result = data.map((p,i,data)=>{
  let item = {
    price:p.price,
    ma:null
  };
  if(i>=2) item.ma = $list(data).last(3).map(p=>p.price).average();
  return item;
})
				
Examples
The second example calculates the moving averages as an array of numbers, and then appends those numbers onto each record through the map mehtod. This method is generally quicker because the calculation of the moving average array can make use of prior calculations.


let ma = await import('/lib/time-series/moving-average/v1.0.0/moving-average.mjs');

let data = [{price:100},{price:101},{price:100},{price:102},{price:102},{price:103},{price:105},{price:102}];
let maData = ma.movingAverage(data.map(p=>p.price),3);

let result = data.map((p,i)=>{
  return {
    price:p.price,
    ma:maData[i]
  }
})
				
Try it!

Examples
The merge method of list functions in a like manner to the example above, in that it takes a calculated array of values and appends them to the objects in the list.


let ma = await import('/lib/time-series/moving-average/v1.0.0/moving-average.mjs');

let data = [{price:100},{price:101},{price:100},{price:102},{price:102},{price:103},{price:105},{price:102}];

let result = $list(data).merge(data=>ma.movingAverage(data.map(p=>p.price),3), 'ma');
				
Try it!

Contents