-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2675-ArrayOfObjectsToMatrix.js
More file actions
246 lines (226 loc) · 5.58 KB
/
2675-ArrayOfObjectsToMatrix.js
File metadata and controls
246 lines (226 loc) · 5.58 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 2675. Array of Objects to Matrix
// 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:
// arr is a valid JSON array
// 1 <= arr.length <= 1000
// unique keys <= 1000
/**
* @param {Array} arr
* @return {(string | number | boolean | null)[][]}
*/
var jsonToMatrix = function(arr) {
// 判读是否是对象
const isObject = x => (x !== null && typeof x === 'object')
// 获取所有的 key
const getKeys = (object) => {
if (!isObject(object)) return [''];
const result = [];
for (const key of Object.keys(object)) {
const childKeys = getKeys(object[key]);
for (const childKey of childKeys) {
result.push(childKey ? `${key}.${childKey}` : key);
}
}
return result;
}
const keysSet = arr.reduce((acc, curr) => {
getKeys(curr).forEach((k) => acc.add(k));
return acc;
}, new Set());
const keys = Array.from(keysSet).sort();
const getValue = (obj, path) => {
const paths = path.split('.')
let i = 0;
let value = obj
while (i < paths.length) {
if (!isObject(value)) break;
value = value[paths[i++]]
}
if (i < paths.length || isObject(value) || value === undefined) return ''
return value
}
const matrix = [keys]
arr.forEach(obj => {
matrix.push(keys.map(key => getValue(obj, key)))
})
return matrix
};
// Example 1:
console.log(jsonToMatrix(
[
{"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:
console.log(jsonToMatrix(
[
{"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:
console.log(jsonToMatrix(
[
{"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:
console.log(jsonToMatrix(
[
[{"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:
console.log(jsonToMatrix(
[
{},
{},
{},
]
));
// Output:
// [
// [],
// [],
// [],
// []
// ]