Adjacent Record Mapping

Overview


An adjacent record map is a transformation over a record, and the record just prior (or after) the record in the array. (this can be extended to any two records in the array).

The map function takes three parameters

  • Current item
  • Current index
  • The array

To calculate the adjacent record map, the code should pass in all three parameters, and then use the index to retrieve the adjacent record.
Examples

The following examples calculates a return for each record and appends it to the record. The first record in the array cannot have a return, so it is set to null in this case.


let data = [{value:1},{value:2},{value:3},{value:4},{value:5}];
let results = data.map((p,i, data)=>{
  let item = {
    value:p.value,
    return:null
  };
  if(i>0) item.return = p.value/data[i-1].value;
  return item;
});
				
Try it!