new | const extractValues = (
fields: string[],
data: DataItemType[]
): Record<string, string[]> => {
if (!fields.length) {
return {};
}
const fieldsWithValues: Record<string, string[]> = {};
for (let item of data) {
for (let field of fields) {
fieldsWithValues[field] = fieldsWithValues[field] || [];
const val = getFieldsByPath(item, field).join(', ');
fieldsWithValues[field].push(val);
}
}
Object.entries(fieldsWithValues).forEach(([field, values]) => {
fieldsWithValues[field] = sortArrayOfStringsByDefaultOrder(
values,
field,
null
);
});
return fieldsWithValues;
};
const mockItems = [
{ a: 'a1', b: 'b1', c: 'c1' },
{ a: 'a2', b: 'b2', c: 'c2' },
{ a: 'a3', b: 'b3', c: 'c3' },
];
const extracted = extractValues(['a', 'b'], mockItems);
| ready |
old | const extractValues = (
fields: string[],
data: DataItemType[]
): Record<string, string[]> => {
if (!fields.length) {
return {};
}
const fieldsWithValues = data.reduce(
(acc, item) =>
fields.reduce((acc, field) => {
if (!acc.has(field)) {
acc.set(field, new Set());
}
acc.get(field)!.add(getFieldsByPath(item, field).join(', '));
return acc;
}, acc),
new Map()
);
const entries = [...fieldsWithValues.entries()].map(([field, values]) => [
field,
sortArrayOfStringsByDefaultOrder([...values], field, null),
]);
return Object.fromEntries(entries);
};
const mockItems = [
{ a: 'a1', b: 'b1', c: 'c1' },
{ a: 'a2', b: 'b2', c: 'c2' },
{ a: 'a3', b: 'b3', c: 'c3' },
];
const extracted = extractValues(['a', 'b'], mockItems);
| ready |