pick-objects-using-id-index-array
v1.0.8
Published
A tiny type safe utility to sort an array of objects by a property using the values array
Downloads
6
Readme
What
A tiny type safe utility to sort an array of objects by a property using the values array.
How
npm i pick-objects-using-id-index-array
import { pickObjects } from 'pick-objects-using-id-index-array';
interface Digit {
id: number;
title: string;
}
const digits: Digit[] = [
{ id: 0, title: 'Zero' },
{ id: 1, title: 'One' },
{ id: 2, title: 'Two' },
{ id: 3, title: 'Three' },
{ id: 4, title: 'Four' },
{ id: 5, title: 'Five' },
];
const sorted = pickObjects([3,2,1], digits, 'id');
console.log(sorted);
// output: [ { id: 3, title: 'Three' }, { id: 2, title: 'Two' }, { id: 1, title: 'One' } ]
Nested or computed/complex ids
For advanced cases idField
param can be a function (receiving the object) - return the value that you use in your index array:
pickObjects([3,2,1], digits, (d) => d.id);
💡 missing items (whose ids weren't matched/found) will be ignored/skipped
Type safe 😌
idField
must either exist in the object type (inferred from the objects
array), ids
array items must be of id field type