[
{name: 'Иван', age: 10},
{name: 'Таня', age: 15},
{name: 'Саша', age: 20},
{name: 'Таня', age: 15},
]
[
{id: 1, name: 'Иван'},
{id: 2, name: 'Таня'},
{id: 3, name: 'Саша'},
]
[
{id: 1, age: '10'},
{id: 2, age: '15'},
{id: 3, age: '20'},
]
const unique = Object.values(arr.reduce((acc, n) => (acc[n.name] = n, acc), {}));
const names = unique.map(({ name }, i) => ({ id: -~i, name }));
const ages = unique.map((n, i) => ({ id: i + 1, age: n.age }));
const extractUnique = (arr, idKey, keys) =>
arr.reduce((acc, n) => {
if (!acc[1].has(n[idKey])) {
const id = acc[1].add(n[idKey]).size;
keys.forEach((k, i) => acc[0][i].push({ id, [k]: n[k] }));
}
return acc;
}, [ keys.map(() => []), new Set ])[0];
const [ names, ages ] = extractUnique(arr, 'name', [ 'name', 'age' ]);