LeetCode/problems/js/array-of-objects-to-matrix.js
Matej Focko f2bbcdd10a
problems(js): add “2675. Array of Objects to Matrix”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-04 00:03:48 +02: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;
};