Hone logo
Hone
Problems

Convert Any Iterable to a JavaScript Array

This challenge focuses on converting various iterable objects (like strings, NodeLists, arguments objects, and Maps) into standard JavaScript arrays. This is a common task when you need to manipulate data from these iterable sources using array methods or when you need a consistent data structure for further processing.

Problem Description

You are tasked with creating a function called iterableToArray that accepts a single argument, iterable. This argument can be any JavaScript iterable object – a string, a NodeList returned by document.querySelectorAll, the arguments object within a function, a Map, a Set, or any other object that implements the iterable protocol. The function should return a new JavaScript array containing all the elements from the input iterable in the order they appear.

Key Requirements:

  • The function must handle any iterable object.
  • The returned array must contain all elements from the iterable.
  • The order of elements in the array must match the order in the iterable.
  • The function should not modify the original iterable.
  • The function should return a new array.

Expected Behavior:

The function should iterate through the input iterable and extract each element, adding it to a new array. The resulting array should be returned.

Edge Cases to Consider:

  • Empty iterables (e.g., an empty string, an empty NodeList).
  • Iterables with non-primitive values (e.g., objects within the iterable).
  • Iterables that might have unusual iteration behavior (though this is less common).
  • Input that is not iterable (though the prompt specifies it is iterable, consider how you might handle this gracefully - returning an empty array is a reasonable approach).

Examples

Example 1:

Input: "hello"
Output: ["h", "e", "l", "l", "o"]
Explanation: The string "hello" is iterable. The function iterates through the string, extracting each character and adding it to a new array.

Example 2:

Input: [1, 2, 3] // Array is also iterable
Output: [1, 2, 3]
Explanation: An array is iterable. The function iterates through the array, extracting each element and adding it to a new array.

Example 3:

Input: new Map([['a', 1], ['b', 2]])
Output: [['a', 1], ['b', 2]]
Explanation: A Map is iterable. The function iterates through the Map, extracting each key-value pair (as an array) and adding it to a new array.

Example 4:

Input: "" // Empty string
Output: []
Explanation: An empty string is iterable, but contains no characters. The function returns an empty array.

Constraints

  • The input iterable will always be an iterable object as defined by the JavaScript iterable protocol.
  • The function must be implemented in JavaScript.
  • The time complexity should be O(n), where n is the number of elements in the iterable.
  • The space complexity should be O(n) due to the creation of the new array.

Notes

Consider using the spread syntax (...) or Array.from() to simplify the conversion process. Remember that the goal is to create a new array, so avoid modifying the original iterable. Think about how the iterable protocol works and how to leverage it to extract the elements.

Loading editor...
javascript