Return item id for item and item type

I’m trying to get the ids of a bunch of items outside of a review session and the only things I have to reference the item is the item itself and the type. Is this possible?

Something like: get_id(“執行猶予”, “vocabulary”) → [item id]

The only way I can think of doing it it checking through every single item, but my knowledge of the API isn’t great so if there’s a more straightforward way, it would be much more convenient.

If you are using the Open Framework and need to do this for a bunch of items, you could let the Open Framework create an index for looking up items by their slug – after that (if there is any ambiguity left) you just need to choose the item that matches the type:

const items = await wkof.ItemData.get_items();
const bySlug = wkof.ItemData.get_index(items, "slug");

function get_id(slug, type) {
	const matches = bySlug[slug];
	if (Array.isArray(matches)) {
		return matches.find(m => m.object === type)?.id;
	} else {
		return matches?.id;
	}
}

I’m assuming this is for your Review Answer History script? In case you want advice/suggestions more specific to your problem, feel free to elaborate what exactly you want to do :slight_smile:

4 Likes

Thanks! And yes it is, I needed to add a check when loading the cached data in order to convert the item object names to their ids rather than the slug. It’d just be a one time thing for compatibility with the updated script but that’s still potentially several hundred items it would need to check for which could end up taking a moment.

2 Likes