paulroth3d / jupyter-ijavascript-utils

Utility library for working with iJavaScript - a Jupyter Kernel
1 stars 0 forks source link

Array.generateSequenceNumber #31

Closed paulroth3d closed 1 year ago

paulroth3d commented 1 year ago

Considering a hierarchy of records, where there is a concatenated key, it would be helpful to know the local sequence number within that group

Must ask for

paulroth3d commented 1 year ago

turns out it isn't necessary, we already have a sourceMap.map() method.

weather = [
  { id: 1, city: 'Seattle',  month: 'Aug', precip: 0.87, dateTime: new Date(2020, 7, 1)  , year: 2020 },
  { id: 0, city: 'Seattle',  month: 'Apr', precip: 2.68, dateTime: new Date(2021, 3, 1)  , year: 2021 },
  { id: 2, city: 'Seattle',  month: 'Dec', precip: 5.31, dateTime: new Date(2020, 11, 1) , year: 2020 },
  { id: 3, city: 'New York', month: 'Apr', precip: 3.94, dateTime: new Date(2021, 3, 1)  , year: 2021 },
  { id: 4, city: 'New York', month: 'Aug', precip: 4.13, dateTime: new Date(2020, 7, 1)  , year: 2020 },
  { id: 5, city: 'New York', month: 'Dec', precip: 3.58, dateTime: new Date(2020, 11, 1) , year: 2020 },
  { id: 6, city: 'Chicago',  month: 'Apr', precip: 3.62, dateTime: new Date(2021, 3, 1)  , year: 2021 },
  { id: 8, city: 'Chicago',  month: 'Dec', precip: 2.56, dateTime: new Date(2020, 11, 1) , year: 2020 },
  { id: 7, city: 'Chicago',  month: 'Aug', precip: 3.98, dateTime: new Date(2020, 7, 1)  , year: 2020 }
];

weatherGroups = utils.group.by(weather, 'city')
    //-- sort 
    .map((cityList) => cityList.sort(utils.array.createSort('-dateTime')))
    //-- add in index
    .map(cityList => cityList.forEach((r,i) => r.index = i));

weather[0];

/*
{
  id: 1,
  city: 'Seattle',
  month: 'Aug',
  precip: 0.87,
  dateTime: 2020-08-01T00:00:00.000Z,
  year: 2020,
  index: 2
}
*/