Coerce a given value to an array.
The function checks if the input value is an array-like or iterable object (like a string, Set, or Map). If so, it returns the value as an array. If the value is not iterable, it returns an array containing the value.
The value to convert into an array.
An array containing the elements of the iterable if the value is iterable, or the value itself if it's not iterable.
const arr1 = toArray([1, 2, 3]);console.log(arr1); // [1, 2, 3]const arr2 = toArray('hello');console.log(arr2); // ['h', 'e', 'l', 'l', 'o']const arr3 = toArray(42);console.log(arr3); // [42] Copy
const arr1 = toArray([1, 2, 3]);console.log(arr1); // [1, 2, 3]const arr2 = toArray('hello');console.log(arr2); // ['h', 'e', 'l', 'l', 'o']const arr3 = toArray(42);console.log(arr3); // [42]
Coerce a given value to an array.
The function checks if the input value is an array-like or iterable object (like a string, Set, or Map). If so, it returns the value as an array. If the value is not iterable, it returns an array containing the value.