Array of Objects to Matrix Problem
Description
LeetCode Problem 2675.
Write a function that converts an array of objects arr into a matrix m.
arr is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and null values.
The first row m should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names are the respective paths in the object separated by ".".
Each of the remaining rows corresponds to an object in arr. Each value in the matrix corresponds to a value in an object. If a given object doesn't contain a value for a given column, the cell should contain an empty string "".
The columns in the matrix should be in lexographically ascending order.
Example 1:
Input:
arr = [
{"b": 1, "a": 2},
{"b": 3, "a": 4}
]
Output:
[
["a", "b"],
[2, 1],
[4, 3]
]
Explanation:
There are two unique column names in the two objects: "a" and "b".
"a" corresponds with [2, 4].
"b" coresponds with [1, 3].
Example 2:
Input:
arr = [
{"a": 1, "b": 2},
{"c": 3, "d": 4},
{}
]
Output:
[
["a", "b", "c", "d"],
[1, 2, "", ""],
["", "", 3, 4],
["", "", "", ""]
]
Explanation:
There are 4 unique column names: "a", "b", "c", "d".
The first object has values associated with "a" and "b".
The second object has values associated with "c" and "d".
The third object has no keys, so it is just a row of empty strings.
Example 3:
Input:
arr = [
{"a": {"b": 1, "c": 2}},
{"a": {"b": 3, "d": 4}}
]
Output:
[
["a.b", "a.c", "a.d"],
[1, 2, ""],
[3, "", 4]
]
Explanation:
In this example, the objects are nested. The keys represent the full path to each value separated by periods.
There are three paths: "a.b", "a.c", "a.d".
Example 4:
Input:
arr = [
[{"a": null}],
[{"b": true}],
[{"c": "x"}]
]
Output:
[
["0.a", "0.b", "0.c"],
[null, "", ""],
["", true, ""],
["", "", "x"]
]
Explanation:
Arrays are also considered objects with their keys being their indices.
Each array has one element so the keys are "0.a", "0.b", and "0.c".
Example 5:
Input:
arr = [
{},
{},
{},
]
Output:
[
[],
[],
[],
[]
]
Explanation:
There are no keys so every row is an empty array.
Constraints:
arris a valid JSON array1 <= arr.length <= 1000unique keys <= 1000
Solution
Approach: Flatten each item, then build the matrix
First turn every item of arr into a flat map from path to leaf value. A recursive walk does this: objects and arrays (arrays use their indices as keys) are descended into, extending the path with ".", and anything else (number, string, boolean, null) is a leaf stored under the current path.
The header is the set of all paths over all items, sorted. Each remaining row looks up every header column in that item's map and writes "" when the path is absent. A value of null is a real value, so test with in rather than truthiness. An empty object has no leaves, which gives an empty row of "" cells (or an empty header and empty rows when every item is empty).
Complexity: Time O(L log L + n * C) where L is the total number of leaves, n the number of items and C the number of columns; space O(n * C).
var jsonToMatrix = function (arr) {
// Collect { "a.b.c": leaf } pairs for one item.
const flatten = (value, path, out) => {
if (value !== null && typeof value === 'object') {
for (const key of Object.keys(value)) {
flatten(value[key], path === '' ? key : path + '.' + key, out);
}
} else {
out[path] = value;
}
return out;
};
const rows = arr.map(item => flatten(item, '', {}));
const columns = [...new Set(rows.flatMap(row => Object.keys(row)))].sort();
const matrix = [columns];
for (const row of rows) {
matrix.push(columns.map(col => (col in row ? row[col] : '')));
}
return matrix;
};