0.1.1 • Published 1 year ago

@afoot/index-lookup v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Index Lookup

An ESM module for generating a lookup index from an array of object literals.

Made this specifically when working with MongoDB documents and pairing reference IDs between query results.

usage

import { indexLookup } from '@afoot/index-lookup';
const fetchResults = [
  {
    _id: 'abc',
    name: 'Tacos',
    ingredients: ['beans', 'cheese'],
  },
  {
    _id: 'def',
    name: 'Pizza',
    ingredients: ['tomatoes', 'cheese'],
  },
];

const lookup = indexLookup('_id', fetchResults);
console.log(lookup); // Returns ['abc','def'];
// You now have a quick lookup to find corresponding objects.
const otherResults = [
    {
      name: 'Tom',
      favoriteFoodId: 'def'
    };
  ]
const foodId = lookup.indexOf(otherResults[0].favoriteFood); // returns 1
const favoriteFoodName = fetchResults[foodId].name; // Returns `Pizza`

license

MIT © Matthew Smith

made with ❤️ and ☕️ by

Niftiness Afoot! Matthew Smith

0.1.1

1 year ago

0.1.0

1 year ago