Hone logo
Hone
Problems

JavaScript: Convert to Array

This challenge focuses on a fundamental data manipulation task in JavaScript: converting various data structures into arrays. Mastering this skill is crucial for processing and transforming data efficiently, enabling you to work with collections of items in a standardized format.

Problem Description

Your task is to create a JavaScript function, convertToJavaScriptArray, that takes a single argument, input, and returns a JavaScript array. This function should be robust enough to handle different types of input and convert them into a predictable array format.

Key Requirements:

  1. Handle Primitive Types: If the input is a primitive type (string, number, boolean, null, undefined, symbol, bigint), convert it into an array containing that single primitive value.
  2. Handle Arrays: If the input is already an array, return it as is (or a shallow copy if immutability is preferred, though for this problem, returning the original is acceptable).
  3. Handle Objects: If the input is a plain JavaScript object (not an array, null, or other built-in object types like Date), convert it into an array of its own enumerable property values. The order of values in the output array should correspond to the order returned by Object.values().
  4. Handle Other Iterable Objects: If the input is an iterable object (e.g., Map, Set, NodeList, Arguments object), convert it into an array by iterating over its elements.
  5. Handle null and undefined: Convert null and undefined into an empty array.

Expected Behavior:

The function should always return a JavaScript array. The contents of the array will depend on the type and content of the input.

Edge Cases to Consider:

  • Empty strings, empty arrays, and empty objects.
  • Objects with Symbol keys.
  • Objects with non-enumerable properties.
  • Special built-in objects that might be iterable but aren't plain objects or standard iterables (e.g., Math, JSON). Your function should handle these gracefully, likely by treating them as primitives if they don't fit other categories.

Examples

Example 1:

Input: "Hello"
Output: ["Hello"]
Explanation: A string is treated as a primitive and converted into an array containing itself.

Example 2:

Input: [1, 2, 3]
Output: [1, 2, 3]
Explanation: The input is already an array, so it's returned directly.

Example 3:

Input: { name: "Alice", age: 30 }
Output: ["Alice", 30]
Explanation: The object's enumerable property values are extracted into an array. The order may vary depending on JavaScript engine specifics for older object definitions, but for modern JS, it generally follows insertion order.

Example 4:

Input: new Set([1, 2, 1, 3])
Output: [1, 2, 3]
Explanation: A Set is an iterable, and its elements are collected into an array.

Example 5:

Input: null
Output: []
Explanation: null is converted to an empty array.

Example 6:

Input: undefined
Output: []
Explanation: undefined is converted to an empty array.

Example 7:

Input: 123
Output: [123]
Explanation: A number is treated as a primitive and converted into an array containing itself.

Constraints

  • The input input can be any valid JavaScript value.
  • The function should not modify the original input.
  • The solution should be reasonably efficient for typical JavaScript data sizes.

Notes

  • Consider using typeof and instanceof operators to differentiate between various data types.
  • Remember that typeof null returns "object", so you'll need an additional check for null.
  • JavaScript provides built-in methods like Object.values() and spread syntax (...) that can be very helpful here.
  • Think about how to distinguish between plain objects and other object types like arrays or Maps.
  • For iterables, you might consider Array.from() or the spread syntax.
Loading editor...
javascript