-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2675-array-of-objects-to-matrix.js
More file actions
63 lines (56 loc) · 2.01 KB
/
2675-array-of-objects-to-matrix.js
File metadata and controls
63 lines (56 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* 2675. Array of Objects to Matrix
* https://leetcode.com/problems/array-of-objects-to-matrix/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {Array} arr
* @return {(string | number | boolean | null)[][]}
*/
var jsonToMatrix = function(arr) {
const allKeys = Array.from(
arr.reduce((keySet, item) => {
extractKeys(item).forEach(key => keySet.add(key));
return keySet;
}, new Set())
).sort();
return [allKeys, ...arr.map(item => allKeys.map(key => getValue(item, key)))];
function isObject(value) {
return value !== null && typeof value === 'object';
}
function extractKeys(object) {
if (!isObject(object)) return [''];
const keys = [];
for (const key of Object.keys(object)) {
const childKeys = extractKeys(object[key]);
for (const childKey of childKeys) {
keys.push(childKey ? `${key}.${childKey}` : key);
}
}
return keys;
}
function getValue(obj, path) {
const segments = path.split('.');
let current = obj;
let index = 0;
while (index < segments.length && isObject(current)) {
current = current[segments[index++]];
}
return index < segments.length || isObject(current) || current === undefined ? '' : current;
}
};