Skip to content

Instantly share code, notes, and snippets.

@carlhannes
Last active October 19, 2019 14:20
Show Gist options
  • Save carlhannes/821123efd111503c28c427ffdc3f1f25 to your computer and use it in GitHub Desktop.
Save carlhannes/821123efd111503c28c427ffdc3f1f25 to your computer and use it in GitHub Desktop.
function JSON2Matrix(jsonArray) {
let matrixHeader = [];
let matrixData = [];
for (let idx = 0; idx < jsonArray.length; idx++) {
const cur = jsonArray[idx];
const keys = Object.keys(cur);
let matrixLine = [];
for (let idk = 0; idk < keys.length; idk++) {
const key = keys[idk];
const posInHeader = matrixHeader.indexOf(key);
if (posInHeader === -1) {
matrixHeader.push(key);
matrixLine[matrixHeader.length - 1] = cur[key];
} else {
matrixLine[posInHeader] = cur[key];
}
}
matrixData.push(matrixLine);
}
return [
matrixHeader,
...matrixData
]
}
export function JSON2Matrix(jsonArray) {
let matrixHeader = [];
let matrixData = [];
function flattenObject(obj, output = {}, curpath = '') {
const keys = Object.keys(obj);
for (let ix1 = 0; ix1 < keys.length; ix1++) {
const key = keys[ix1];
const val = obj[key];
if (typeof val === 'object' && val !== null) {
flattenObject(val, output, `${curpath}${curpath ? '/' : ''}${key}`);
} else {
output[`${curpath}${curpath ? '/' : ''}${key}`] = val;
}
}
return output;
}
for (let idx = 0; idx < jsonArray.length; idx++) {
const cur = flattenObject(jsonArray[idx]);
const keys = Object.keys(cur);
let matrixLine = [];
for (let idk = 0; idk < keys.length; idk++) {
const key = keys[idk];
const posInHeader = matrixHeader.indexOf(key);
if (posInHeader === -1) {
matrixHeader.push(key);
matrixLine[matrixHeader.length - 1] = cur[key];
} else {
matrixLine[posInHeader] = cur[key];
}
}
matrixData.push(matrixLine);
}
return [matrixHeader, ...matrixData];
}
import extend from 'extend';
export function JSON2Matrix(jsonArray) {
let matrixHeader = [];
let matrixData = [];
function flattenObject(obj, { rootObj = null, output = [{}], curindex = 0, curpath = '' } = {}) {
const keys = Object.keys(obj);
let arrProcessQueue = [];
for (let ix1 = 0; ix1 < keys.length; ix1++) {
const key = keys[ix1];
const val = obj[key];
const genpath = `${curpath}${curpath ? '/' : ''}${key}`;
if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
flattenObject(val, { output, curindex, curpath: genpath });
} else if (Array.isArray(val)) {
console.log('array', val);
val.forEach((item, arridx) => {
if (arridx === 0) {
flattenObject(item, { output, curindex, curpath: genpath });
} else {
arrProcessQueue.push({
item, genpath
});
}
})
} else {
output[curindex][genpath] = val;
}
}
arrProcessQueue.forEach((queueItem) => {
output.push(extend(true, {}, output[curindex]));
flattenObject(queueItem.item, { output, curindex: output.length - 1, curpath: queueItem.genpath });
});
return output;
}
for (let idx = 0; idx < jsonArray.length; idx++) {
const objs = flattenObject(jsonArray[idx])
objs.forEach((cur) => {
const keys = Object.keys(cur);
let matrixLine = [];
for (let idk = 0; idk < keys.length; idk++) {
const key = keys[idk];
const posInHeader = matrixHeader.indexOf(key);
if (posInHeader === -1) {
matrixHeader.push(key);
matrixLine[matrixHeader.length - 1] = cur[key];
} else {
matrixLine[posInHeader] = cur[key];
}
}
matrixData.push(matrixLine);
})
}
return [matrixHeader, ...matrixData];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment