Summary
liniarGroup accepts an array, and a mapping function.
It will output an array of objects describing the index ranges of contiguous sequences, based on the mapping function.
Usage
A Simple Example
window.BN.liniarGroup([0, 0, 1, 2, 2], (v) => v);
results in:
0 - 1, 0
2, 1
3, 2
mapped value: 0, range: "0 - 1",
mapped value: 1, range: "2"
mapped value: 2, range: "3 - 4"
note: console.table() is useful for displaying this data
A More Complex Example
------------------------
window.BN.liniarGroup(phonebook.entries, (v) => {
if (!v.getName()) return "undefined";
return v.getName()["Last Name"];
});
results in:
range: "0 - 50", mapped value: "Anderson"
range: "51-55", mapped value: "Appleton"
range: "56-57", mapped value: "Avery"
range: "60", mapped value: "Axelrod"
Code
window.BN = window.BN || {};
window.BN.liniarGroup = (arraryIn, fnMap) => {
let results = [];
let prevIdx = 0;
let prevVal = fnMap(arraryIn[0]);
let lastFoundIndex = undefined;
for(var i=1; i<arraryIn.length; i++) {
let iter = arraryIn[i];
let mappedVal = fnMap(iter);
if (mappedVal === prevVal) {
}
else {
let range = prevIdx === (i-1) ? prevIdx.toString() : prevIdx + " - " + (i-1);
results.push({"range" : range, "mapped value": prevVal});
prevVal = mappedVal;
prevIdx = i;
}
}
let range = prevIdx === (i-1) ? prevIdx.toString() : prevIdx + " - " + (i-1);
results.push({"range" : range, "mapped value": prevVal});
return results;
}
No comments:
Post a Comment