Menu
Coddy logo textTech

Function to Find an Item by ID

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 65 of 73.

challenge icon

Challenge

Easy

You are provided with the following from the previous challenge:

  • The generic interface InventoryItem<T> with properties id, quantity, and details
  • The generic function addItem that adds items to inventory arrays
  • Various inventory items and updated inventory arrays

Create a generic function named findItemById that searches for an item in an inventory array by its ID.

The function should:

  • Use a generic type parameter T
  • Accept a parameter inventory of type InventoryItem<T>[]
  • Accept a parameter id of type number
  • Return the found item of type InventoryItem<T> or undefined if not found
  • Have an explicit return type annotation of InventoryItem<T> | undefined

Create test inventories:

  • Create mixedBookInventory as an array containing:
    • An item with id: 10, quantity: 3, details: { title: "JavaScript Basics", author: "Alice Brown" }
    • An item with id: 11, quantity: 7, details: { title: "React Fundamentals", author: "Bob Wilson" }
    • An item with id: 12, quantity: 2, details: { title: "Node.js Guide", author: "Carol Davis" }
  • Create mixedElectronicInventory as an array containing:
    • An item with id: 20, quantity: 5, details: { brand: "Samsung", model: "Galaxy S23" }
    • An item with id: 21, quantity: 1, details: { brand: "Apple", model: "iPhone 15" }

Test your function and print the following outputs:

  • Find item with ID 11 in mixedBookInventory and print the result's details.title (or undefined if not found)
  • Find item with ID 20 in mixedElectronicInventory and print the result's details.brand (or undefined if not found)
  • Find item with ID 99 in mixedBookInventory and print the result (should be undefined)
  • Find item with ID 12 in mixedBookInventory and print the result's quantity (or undefined if not found)
  • Find item with ID 21 in mixedElectronicInventory and print the result's details.model (or undefined if not found)
  • Find item with ID 10 in mixedBookInventory and print the result's id (or undefined if not found)
  • Find item with ID 50 in mixedElectronicInventory and print the result (should be undefined)

Try it yourself

// Generic interface from previous challenge
interface InventoryItem<T> {
  id: number;
  quantity: number;
  details: T;
}

// Items from previous challenge
const bookItem: InventoryItem<{ title: string; author: string }> = {
  id: 1,
  quantity: 5,
  details: { title: "TypeScript Handbook", author: "John Doe" }
};

const electronicItem: InventoryItem<{ brand: string; model: string }> = {
  id: 2,
  quantity: 3,
  details: { brand: "TechCorp", model: "X100" }
};

const clothingItem: InventoryItem<{ size: string; color: string }> = {
  id: 3,
  quantity: 10,
  details: { size: "M", color: "Blue" }
};

// Generic function to add item to inventory
function addItem<T>(inventory: InventoryItem<T>[], newItem: InventoryItem<T>): InventoryItem<T>[] {
  return [...inventory, newItem];
}

// Create book inventory and new book
const bookInventory = [bookItem];
const newBook: InventoryItem<{ title: string; author: string }> = {
  id: 4,
  quantity: 2,
  details: { title: "Advanced TypeScript", author: "Jane Smith" }
};
const updatedBookInventory = addItem(bookInventory, newBook);

// Create electronic inventory and new electronic
const electronicInventory = [electronicItem];
const newElectronic: InventoryItem<{ brand: string; model: string }> = {
  id: 5,
  quantity: 1,
  details: { brand: "GadgetCorp", model: "Z500" }
};
const updatedElectronicInventory = addItem(electronicInventory, newElectronic);

// Print outputs
console.log(updatedBookInventory.length);
console.log(updatedBookInventory[1].details.title);
console.log(updatedBookInventory[1].details.author);
console.log(updatedElectronicInventory.length);
console.log(updatedElectronicInventory[1].details.brand);
console.log(updatedElectronicInventory[1].details.model);
console.log(updatedElectronicInventory[0].id);
console.log(updatedElectronicInventory[1].quantity);

All lessons in Introduction To TypeScript