pick-objects-using-id-index-array
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i pick-objects-using-id-index-array

Weekly Downloads

7

Version

1.0.6

License

ISC

Unpacked Size

6.86 kB

Total Files

10

Last publish

Collaborators

  • roman_js