weighted-values.ts (395B)
1 import { WeightedValue } from './weighted-value'; 2 3 export function weightedValues(arr: any[]): WeightedValue[] { 4 const storage: WeightedValue[] = []; 5 for(const value of arr) { 6 let found: WeightedValue = storage.find(a => a.value === value); 7 if (found) { 8 found.weight++; 9 } else { 10 storage.push({ 11 weight: 1, 12 value 13 }); 14 } 15 } 16 return storage; 17 }