xxxxxxxxxx
const getSchemaFields = (schema, result) => {
if (Array.isArray(schema)) {
for (var i = 0; i < schema.length; i++) {
getSchemaFields(schema[i], result);
}
} else {
for (var prop in schema) {
if (['number'].includes(schema[prop].type)) {
result.push(prop);
}
if (typeof schema[prop] == 'object' || Array.isArray(schema[prop])) {
getSchemaFields(schema[prop], result);
}
}
}
return result;
}
const schema1 = {
"amount": {
"type": "number"
},
"invoice_url": {
"type": "Object",
"detail": {
"type": "number"
}
}
}
const fields = getSchemaFields(schema1, []);
console.log(fields)
xxxxxxxxxx
function getObject(theObject) {
var result = null;
if(theObject instanceof Array) {
for(var i = 0; i < theObject.length; i++) {
result = getObject(theObject[i]);
if (result) {
break;
}
}
}
else
{
for(var prop in theObject) {
console.log(prop + ': ' + theObject[prop]);
if(prop == 'id') {
if(theObject[prop] == 1) {
return theObject;
}
}
if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
result = getObject(theObject[prop]);
if (result) {
break;
}
}
}
}
return result;
}