Skip to content

Instantly share code, notes, and snippets.

@ryousaf
Created May 3, 2021
Embed
What would you like to do?
Trial Script to map any object/array to an structure object for easier interface mapping
console.log("structure", this.fetchObjectStructure([
{
attributes: [ 1, 2, 3 ]
},
{
attributes: [ "a", "b" ]
}
]));
fetchObjectStructure(data: any) {
if(data === null) {
return "null";
}
switch(data.__proto__.constructor.name) {
case 'Object': {
const structure: { [prop: string]: any, [prop: number]: any} = {};
for(let key in data) {
const temp = this.fetchObjectStructure(data[key]);
if(structure[key] === undefined || structure[key] === "null") {
structure[key] = temp;
if(structure[key].__proto__.constructor.name==="String") {
structure[key] = structure[key].toLowerCase() + ";";
}
return;
}
if(structure[key].__proto__.constructor.name === temp.__proto__.constructor.name) {
if(structure[key].__proto__.constructor.name === "Object" && temp.__proto__.constructor.name === "Object") {
Object.assign(structure[key], temp);
} else if(structure[key].__proto__.constructor.name === 'Array' && temp.__proto__.constructor.name === 'Array') {
Object.assign(structure[key][0], temp[0]);
}
} else {
structure[key] = JSON.stringify(structure[key])+" | "+JSON.stringify(temp);
}
}
return structure;
}
case 'Array': {
let structure: any;
for(let item of data) {
const subStructure = this.fetchObjectStructure(item);
if(subStructure.__proto__.constructor.name === 'Object') {
for(let key in subStructure) {
if(structure[key] === undefined || structure[key] === "null") {
structure[key] = subStructure[key];
if(structure[key].__proto__.constructor.name==="String")
structure[key] = structure[key].toLowerCase() + ";";
} else if(structure[key].__proto__.constructor.name === subStructure[key].__proto__.constructor.name) {
if(structure[key].__proto__.constructor.name === "Object" && subStructure[key].__proto__.constructor.name === "Object") {
Object.assign(structure[key], subStructure[key]);
} else if(structure[key].__proto__.constructor.name === 'Array' && subStructure[key].__proto__.constructor.name === 'Array') {
Object.assign(structure[key][0], subStructure[key][0]);
}
} else {
structure[key] = JSON.stringify(structure[key])+" | "+JSON.stringify(subStructure[key]);
}
}
} else if(subStructure.__proto__.constructor.name === 'Array') {
} else {
if(structure === null || structure === undefined || structure === 'null') {
structure = subStructure;
} else if(structure !== subStructure) {
structure += structure+" | "+subStructure;
}
}
}
return [structure];
}
default:
return data.__proto__.constructor.name;
}
}
@ryousaf
Copy link
Author

ryousaf commented May 3, 2021

Script fails and returns a "circular" object with the current temp input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment