Generator design part II

Click the button to generate!

This is the output of the current JS, need to create utility to pick random entry regardless of object name:

Example of potential utility code:

// Example JSON object
const jsonObject = {
  "users": ["Alice", "Bob", "Charlie", "David"],
  "meta": "not-an-array"
};

// 1. Find the array value regardless of key
const dataArray = Object.values(jsonObject).find(Array.isArray);

// 2. Select random item
if (dataArray) {
  const randomItem = dataArray[Math.floor(Math.random() * dataArray.length)];
  console.log(randomItem); // Outputs: "Alice", "Bob", etc.
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top