Skip to content

Instantly share code, notes, and snippets.

@chand1012
Created March 26, 2024 18:50
Show Gist options
  • Save chand1012/5bc27ef2e5c78c27e4387861cde296fd to your computer and use it in GitHub Desktop.
Save chand1012/5bc27ef2e5c78c27e4387861cde296fd to your computer and use it in GitHub Desktop.
Get a random item from an input N8N array. MIT https://chand1012.mit-license.org
function getRandomElement(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('The input must be a non-empty array.');
}
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
const items = $input.all();
const item = getRandomElement(items);
return [item];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment