-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray-of-objects-to-matrix.js
69 lines (67 loc) · 1.8 KB
/
array-of-objects-to-matrix.js
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
64
65
66
67
68
69
// 2675. Array of Objects to Matrix
// 🟠 Medium
//
// https://leetcode.com/problems/array-of-objects-to-matrix/
//
// Tags: Javascript
// Get all the keys and use them to generate the header row of the matrix, then
// go through each object checking its values and adding them to their
// matching columns.
//
// Time complexity: O(n) - We visit each key in the input array, top level and
// nested, at least once.
// Space complexity: O(n) - Even not counting the output data structure, the
// key set could, depending on the shape of the input, have the same size as
// the input.
//
// Runtime 377 ms Beats 82.6%
// Memory 91.6 MB Beats 94.35%
/**
* @param {Array} arr
* @return {Matrix}
*/
var jsonToMatrix = function (arr) {
const keySet = new Set();
// Get the keys for each object.
for (const obj of arr) {
getKeys(obj, "");
}
const keys = Array.from(keySet).sort();
const res = [keys];
for (const obj of arr) {
const ktv = {};
getValues(obj, "", ktv);
const row = [];
for (const key of keys) {
if (key in ktv) {
row.push(ktv[key]);
} else {
row.push("");
}
}
res.push(row);
}
return res;
/** Get all the keys */
function getKeys(obj, path) {
Object.keys(obj).forEach((key) => {
const np = path ? `${path}.${key}` : key;
if (obj[key] !== null && typeof obj[key] === "object") {
getKeys(obj[key], np);
} else {
keySet.add(np);
}
});
}
/** Given all the values that will go in a row of the matrix */
function getValues(obj, path, ktv) {
Object.keys(obj).forEach((key) => {
const np = path ? `${path}.${key}` : key;
if (obj[key] !== null && typeof obj[key] === "object") {
getValues(obj[key], np, ktv);
} else {
ktv[np] = obj[key];
}
});
}
};