Created
March 26, 2024 18:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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