1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/js/array-of-objects-to-matrix.js
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

49 lines
1 KiB
JavaScript

const isObject = x => x !== null && typeof x === 'object';
const getKeys = object => {
if (!isObject(object)) {
return [''];
}
let result = [];
Object.keys(object).forEach(key => {
getKeys(object[key]).forEach(subKey => {
result.push(subKey ? `${key}.${subKey}` : key);
});
});
return result;
};
const getValue = (obj, path) => {
let [value, i, paths] = [obj, 0, path.split('.')];
for (i = 0; i < paths.length && isObject(value); ++i) {
value = value[paths[i]];
}
if (i < paths.length || value === undefined || isObject(value)) {
return '';
}
return value;
};
/**
* @param {Array} arr
* @return {Matrix}
*/
const jsonToMatrix = function(arr) {
let keySet = arr.reduce((keys, key) => {
getKeys(key).forEach(k => keys.add(k));
return keys;
}, new Set());
let keys = Array.from(keySet).sort();
let matrix = [keys];
arr.forEach(obj => {
matrix.push(keys.map(key => getValue(obj, key)));
});
return matrix;
};