I'm new to Node.js, and I wanted to know if there is a way to iterate over the fields of a structure and know their types.
For exemple:
schema {
origin: String,
originDate: Date,
arrival: String,
arrivalDate: Date,
passagers: {
adults: Number,
children: Number
}
}
For example:
I want to loop in the scheema
and know what is the type of each field.
Expected response:
String
Date
String
Date
Object
and also know what that object has.
Answer here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.
The basic logic is to pass a object type to a Object.keys() function which will return the list with all the keys
const user = new User(); // create new object
const schemaKeys = Object.keys(user.toObject());
console.log("The keys are",schemaKeys)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.