Practice/Google/Leetcode 2675. Array of Objects to Matrix
CodingOptional
You are building a data export feature that converts a collection of JavaScript objects into a spreadsheet-like matrix format. Given an array of objects, transform it into a 2D array where:
"") as a placeholderThis is useful for exporting JSON data to CSV format or displaying object collections in tabular form.
"") for missing key-value pairsExample 1:
Input: [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}] Output: [["name", "age"], ["Alice", 30], ["Bob", 25]] Explanation: Both objects have identical keys. The header row is ["name", "age"], and each object becomes a data row.
Example 2:
Input: [{"a": 1, "b": 2}, {"b": 3, "c": 4}] Output: [["a", "b", "c"], [1, 2, ""], ["", 3, 4]] Explanation: Keys appear in order: "a" (from obj1), "b" (from obj1), "c" (from obj2). First object lacks "c", second lacks "a".
Example 3:
Input: [] Output: [] Explanation: An empty input array results in an empty matrix.
Example 4:
Input: [{"x": 1}, {"y": 2}, {"z": 3}] Output: [["x", "y", "z"], [1, "", ""], ["", 2, ""], ["", "", 3]] Explanation: Each object has a unique key. Most cells will be empty strings.